php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43926 isInstance() isn't equivalent to instanceof operator
Submitted: 2008-01-24 13:16 UTC Modified: 2008-01-30 10:30 UTC
From: felipe@php.net Assigned:
Status: Closed Package: Class/Object related
PHP Version: 5.3CVS-2008-01-24 (CVS) OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: felipe@php.net
New email:
PHP Version: OS:

 

 [2008-01-24 13:16 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-30 10:30 UTC] felipe@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 16 22:01:30 2024 UTC