#43926 [Opn]: isInstance() isn't equivalent to instanceof operator
| From: | felipe@php.net | Date: | Thu, 24 Jan 2008 13:23:37 +0000 |
| Subject: | #43926 [Opn]: isInstance() isn't equivalent to instanceof operator | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-121507@lists.php.net to get a copy of this message | ||
ID: 43926
Updated by: felipe@php.net
Reported By: felipe@php.net
Status: Open
Bug Type: Unknown/Other Function
Operating System: Linux
PHP Version: 5.3CVS-2008-01-24 (CVS)
New Comment:
.phpt:
http://ecl.zoone.com.br/etc/patches/bug43926.phpt
Patch:
http://ecl.zoone.com.br/etc/patches/bug43926.patch
Previous Comments:
------------------------------------------------------------------------
[2008-01-24 13:16:07] felipe@php.net
Description:
------------
Says the documentation:
"Note: $class = new ReflectionClass('Foo'); $class->isInstance($arg) is
equivalent to $arg instanceof Foo or is_a($arg, 'Foo')."
However, this isn't true because not is checked the parent class.
Reproduce code:
---------------
<?php
class E {
}
class D extends E {
}
class A extends D {
}
class C extends A {
}
$ra = new ReflectionClass('A');
$rc = new ReflectionClass('C');
$rd = new ReflectionClass('D');
$re = new ReflectionClass('E');
$ca = $ra->newInstance();
$cc = $rc->newInstance();
$cd = $rd->newInstance();
$ce = $re->newInstance();
print("Is? A ". ($ra->isInstance($ca) ? 'true' : 'false') .",
instanceof: ". (($ca instanceof A) ? 'true' : 'false') ."\n");
print("Is? C ". ($ra->isInstance($cc) ? 'true' : 'false') .",
instanceof: ". (($ca instanceof C) ? 'true' : 'false') ."\n");
print("Is? D ". ($ra->isInstance($cd) ? 'true' : 'false') .",
instanceof: ". (($ca instanceof D) ? 'true' : 'false') ."\n");
print("Is? E ". ($ra->isInstance($ce) ? 'true' : 'false') .",
instanceof: ". (($ca instanceof E) ? 'true' : 'false') ."\n");
?>
Expected result:
----------------
Is? A true, instanceof: true
Is? C false, instanceof: false
Is? D true, instanceof: true
Is? E true, instanceof: true
Actual result:
--------------
Is? A true, instanceof: true
Is? C false, instanceof: false
Is? D false, instanceof: true
Is? E false, instanceof: true
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=43926&edit=1