I don't see any mention of "namespaces" on this page so I thought I would chime in. The instanceof operator takes FQCN as second operator when you pass it as string and not a simple class name. It will not resolve it even if you have a `use MyNamespace\Bar;` at the top level. Here is what I am trying to say:

You must use a ReflectionClass :<?php$classname = 'MyClass';$myReflection = new ReflectionClass($classname);if( $myReflection->isSubclassOf('MyParentClass')) echo 'Child of it';else echo 'Not child of it';?>

Will output the good result.If you're testing an interface, use implementsInterface() instead of isSublassOf().

Please note: != is a separate operator with separate semantics. Thinking about language grammar it's kind of ridicilous to negate an operator. Of course, it's possible to negate the result of a function (like is_a()), since it isn't negating the function itself or its semantics.

instanceof is a binary operator, and so used in binary terms like this

terma instanceof termb

while ! (negation) is a unary operator and so may be applied to a single term like this

!term

And a term never consists of an operator, only! There is no such construct in any language (please correct me!). However, instanceof doesn't finally support nested terms in every operand position ("terma" or "termb" above) as negation does:

So - instanceof is smart enough to know that a class that implements an interface is an instance of the interface, not just the class. I didn't see that point made clearly enough in the explanation at the top.

You mentionned "the instanceof operator will not accept a string as its first operand". However, this behavior is absolutely right and therefore, you're misleading the meaning of an instance.

<?php 'ClassA' instanceof 'ClassB'; ?> means "the class named ClassA is an instance of the class named ClassB". This is a nonsense sentence because when you instanciate a class, you ALWAYS obtain an object. Consequently, you only can ask if an object is an instance of a class.

I believe asking if "a ClassA belongs to a ClassB" (or "a ClassA is a class of (type) ClassB") or even "a ClassA is (also) a ClassB" is more appropriate. But the first is not implemented and the second only works with objects, just like the instanceof operator.

Plus, I just have tested your code and it does absolutely NOT do the same as instanceof (extended to classes)! I can't advise anyone to reuse it. The use of <?php is_instance_of ($instanceOfA, 'ClassB'); ?> raises a warning "include_once(Object id #1.php) …" when using __autoload (trying to look for $instanceOfA as if it was a class name).

Finally, here is a fast (to me) sample function code to verify if an object or class:

use this to check instance of or you can add the else part inside the if making it nested if for dual varification.:-<?phpclass MyClass{}class Test extends MyClass{public function checkObjectArray(array $array,$classname,$strict=false){if(!$strict){foreach($array as $element){ if(!($element instanceOf $classname)){//we can use typehint objectsINSIDE an array echo "false"; echo "<br />"; break; } else{ print "<br />true (if)"; }}}else{foreach($array as $element){if(get_class($element)!=$classname){// or we can use this function tocheck the classes inside this arrayecho "false (else)";}}}echo "<br />true";}}$ob=new Test;

Now instantiate a few instances of these types. Note, I will put them in an array (collection) so we can iterate through them quickly.

<?php

$myCollection = array(123, 'abc', 'Hello World!', new Circle(), new Circle(), new Circle(), new Point(), new Point(), new Point());

$i = 0;foreach($myCollection AS $item){/* * The setRadius() function is written in the Circle class * definition above, so make sure $item is an instance of * type Circle BEFORE calling it AND to avoid PHP PMS! */if($item instanceof Circle) {$item->setRadius($i); }

/* * The setLocation() function is written in the Point class * definition above, so make sure $item is an instance of * type Point BEFORE calling it AND to stay out of the ER! */if($item instanceof Point) {$item->setLocation($i, $i); }

The PHP parser generates a parse error on either of the two lines that are commented out here. Apparently the 'instanceof' construct will take a string variable in the second spot, but it will NOT take a string... lame