ID:               29736
 Updated by:       [EMAIL PROTECTED]
 Reported By:      hans at velum dot net
-Status:           Bogus
+Status:           Open
 Bug Type:         Feature/Change Request
 Operating System: *
 PHP Version:      5.0.1
 New Comment:

The example was buggy, but the bug exists :)

php5 -r 'class Foo{} $f = unserialize("O:3:\"Foo\":0:{}");
var_dump(!($foo instanceof Bar));'

Bar could be a huge class, I dont want to load it just to check $f
wasnt a instance of it?


Previous Comments:
------------------------------------------------------------------------

[2004-08-19 02:40:32] hans at velum dot net

Well, no ... the problem that I am describing has nothing to do with
unserialize().  That was just an example that Sean posted that
illustrates how is_a() and instanceof differ.  I apologize if that
wasn't clear.

You did reproduce the bug in your first example.

Simple reproduction:

<?php

$c = null;
print '$c instanceof PEAR_Error ? ' . ($c instanceof PEAR_Error);

?>

Will result in E_FATAL (no such class).  That's the problem.  is_a()
has no such limitations, but is now deprecated.  This change
suggestion/request is asking for a way to check whether a variable is
an instance of a particular class/superclass/interface *without loading
that class/interface*.

Hope that makes it clearer.

-Hans

------------------------------------------------------------------------

[2004-08-19 01:28:46] [EMAIL PROTECTED]

Can't reproduce:

[EMAIL PROTECTED] /usr/src/PHP_5_0 $ php -r 'function __autoload($name) {
var_dump($name); } $x = NULL; var_dump($x instanceof test);'
make: `sapi/cli/php' is up to date.
string(4) "test"

Fatal error: Class 'test' not found in Command line code on line 1
[EMAIL PROTECTED] /usr/src/PHP_5_0 $ php -r 'function __autoload($name) {
var_dump($name); eval("class $name {}");} $x = NULL; var_dump($x
instanceof test);'
make: `sapi/cli/php' is up to date.
string(4) "test"
bool(false)

So the problem lies in unserialize() -> other bug

------------------------------------------------------------------------

[2004-08-18 16:31:23] hans at velum dot net

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 this bug report at http://bugs.php.net/?id=29736&edit=1

Reply via email to