From:             hans at velum dot net
Operating system: All
PHP version:      5.0.1
PHP Bug Type:     Feature/Change Request
Bug description:  instanceof requires class to be loaded

Description:
------------
[pasted from messages on internals@ list]

I like the new $obj instanceof ClassName way of checking class / interface
types, .... but this method does have one major drawback compared to the
old is_a() approach:

  The class must be loaded in order to perform an instanceof check!

There are many situations where you do not want to have to load every
class to see whether a returned object is a class of that type.  In
particular for driver classes, the current model requires loading *every*
possible driver in order to check to see which type of class was
returned.

is_a() works great, but has been deprecated and is now generating E_STRICT
errors.


Reproduce code:
---------------
[conributed by Sean Coates on internals@ list]

<?php

class Foo {}

$foo = unserialize('O:3:"Foo":0:{}');

echo '$foo is_a Foo? '. (is_a($foo, 'Foo') ? 'yes' : 'no') ."\n";
echo '$foo instanceof Foo? '. ($foo instanceof Foo ? 'yes' : 'no') ."\n";

?>

---- and ----

<?php

//class Foo {}

$foo = unserialize('O:3:"Foo":0:{}');

echo '$foo is_a Foo? '. (is_a($foo, 'Foo') ? 'yes' : 'no') ."\n";
echo '$foo instanceof Foo? '. ($foo instanceof Foo ? 'yes' : 'no') ."\n";

?>

Expected result:
----------------
$foo is_a Foo? yes
$foo instanceof Foo? yes

---- and ----

$foo is_a Foo? no
$foo instanceof Foo? no



Actual result:
--------------
$foo is_a Foo? yes
$foo instanceof Foo? yes

---- and ----

$foo is_a Foo? no

Fatal error: Class 'Foo' not found in 
/home/sean/phpdoc/scripts/tmp/is_a.php on line 8

-- 
Edit bug report at http://bugs.php.net/?id=29736&edit=1
-- 
Try a CVS snapshot (php4):  http://bugs.php.net/fix.php?id=29736&r=trysnapshot4
Try a CVS snapshot (php5):  http://bugs.php.net/fix.php?id=29736&r=trysnapshot5
Fixed in CVS:               http://bugs.php.net/fix.php?id=29736&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=29736&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=29736&r=needtrace
Need Reproduce Script:      http://bugs.php.net/fix.php?id=29736&r=needscript
Try newer version:          http://bugs.php.net/fix.php?id=29736&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=29736&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=29736&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=29736&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=29736&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=29736&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=29736&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=29736&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=29736&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=29736&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=29736&r=float

Reply via email to