Usefulness of using class_implements VS instanceof
doctrine-orm, optimization, php
Solution
`class_implements` will return an array of all the interfaces that are implemented by a specified class.
One reason to use `class_implements` over `instanceof` is that you can use `class_implements` on a string that is the name of a class:
<?php
interface HasTest {
public function test();
}
class TestClass implements HasTest {
public function test() {
return 'This is a quick test';
}
}
$test = array (
'test' => 'TestClass'
);
// !! Using instanceof
var_dump($test['test'] instanceof HasTest);
// !! Using class_implements
var_dump(in_array('HasTest', class_implements($test['test'])));
/**
* Output:
*
* bool(false)
* bool(true)
*/
$class = new $test['test'];
var_dump($class instanceof HasTest);
/**
* Output:
* bool(true)
*/
Problem
In Doctrine's source code if stumbled upon the following test: ``` if (in_array('Doctrine\Common\Collections\Collection', class_implements($var))) { // ... } ``` I don't get why not using `instanceof` instead: ``` if ($var instanceof Doctrine\Common\Collections\Collection) { // ... } ``` which is better in many ways. Is there a tangible reason for doing this? Maybe performances? But really, is there any real difference here, it seems to me it would be like simple VS double quotes.