is_a() vs. instanceof

php.internals

Hans Lellelid

22 years ago
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! Apparently is_a() has also been deprecated, so that an E_STRICT error is thrown. This means that to check types of classes and remain E_STRICT compliant I have to load & parse every class. It's not hard to imagine situations (and experience has shown this to be the case) where this limitation can lead to a lot of extra file i/o and parsing -- and also E_WARNING if the file couldn't be included, etc. Is is possible to have instanceof not require the class to be loaded? or perhaps just have is_a() not trigger an E_STRICT error? Thanks, Hans

Dan Ostrowski

22 years ago
On Tuesday 10 August 2004 7:36 pm, Hans Lellelid wrote:
> 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! >
Isn't that the point? is_a() and instanceof are ways of checking the class of an object at runtime where it could be any number of things. ( And here I'm assuming you mean instantiated instead of "loaded"? ) If you don't have an instantiated object.. what do you have? A class! And THAT can be checked at "compile" ( or in this case write ) time. There's something wrong with my code, if I'm thinking of this right, if I have no idea if class A is a subclass of class B. Now, if I'm passed random objects, I may want to know of which types they are... which is where these functions/builtins come in... Am I misunderstanding you? Dan Ostrowski

Alan Knowles

22 years ago
I think he's referening to something like this (which is common in pear): $x = $someobj->somemethod(); if ($x instanceOf PEAR_Error) { ....... } while that code is redundant in the case of exceptions, it is still a valid situation.. that $x may be a valid return, and PEAR_Error was never loaded.. Regards Alan Dan Ostrowski wrote:

Hans Lellelid

22 years ago
Alan Knowles wrote:
> I think he's referening to something like this (which is common in pear): > > $x = $someobj->somemethod(); > if ($x instanceOf PEAR_Error) { > ....... > } > > while that code is redundant in the case of exceptions, it is still a > valid situation.. that $x may be a valid return, and PEAR_Error was > never loaded..
Yup, that's exactly what I'm talking about. I want to be able to do this: if ($db instanceof DBPostgres) { /// do something funky specific to Postgres } If My DB adapter is DBMySQL, I don't want to load the DBPostgres adapter just so I can test whether my object is of that type ... and checking first whether class_exists('DBPostgres') just seems kinda kludgy & straying from the "problem domain" ... especially since $db instanceof DBPostgres would *always* be false if DBPostgres isn't loaded. Hans

Al Baker

22 years ago
I agree, that's the exact behavior I would like as well and what most people would expect. Al On Wed, 2004-08-11 at 23:52 -0400, Hans Lellelid wrote:

Jevon Wright

22 years ago
What about: if (get_class($obj) == "unloadedclass") { // blah } Jevon ----- Original Message ----- From: "Al Baker" <ajb732@comcast.net> To: "Hans Lellelid" <hans@velum.net> Cc: "Dan Ostrowski" <dan@ostrowski.cc>; <internals@lists.php.net> Sent: Thursday, August 12, 2004 9:58 PM Subject: Re: [PHP-DEV] is_a() vs. instanceof
> I agree, that's the exact behavior I would like as well and what most > people would expect. > > Al > > On Wed, 2004-08-11 at 23:52 -0400, Hans Lellelid wrote: > > Alan Knowles wrote: > > > > > I think he's referening to something like this (which is common in
pear):
> > > > > > $x = $someobj->somemethod(); > > > if ($x instanceOf PEAR_Error) { > > > ....... > > > } > > > > > > while that code is redundant in the case of exceptions, it is still a > > > valid situation.. that $x may be a valid return, and PEAR_Error was > > > never loaded.. > > > > > > Yup, that's exactly what I'm talking about. > > > > I want to be able to do this: > > > > if ($db instanceof DBPostgres) { > > /// do something funky specific to Postgres > > } > > > > If My DB adapter is DBMySQL, I don't want to load the DBPostgres adapter > > just so I can test whether my object is of that type ... > > > > and checking first whether class_exists('DBPostgres') just seems kinda > > kludgy & straying from the "problem domain" ... especially since $db > > instanceof DBPostgres would *always* be false if DBPostgres isn't
loaded.

Sean Coates

22 years ago
Dan Ostrowski wrote:
> On Tuesday 10 August 2004 7:36 pm, Hans Lellelid wrote: >> The class must be loaded in order to perform an instanceof check!
*snip*
> Am I misunderstanding you? >
consider the following: <?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"; ?> outputs: $foo is_a Foo? yes $foo instanceof Foo? yes 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"; ?> outputs: $foo is_a Foo? no Fatal error: Class 'Foo' not found in /home/sean/phpdoc/scripts/tmp/is_a.php on line 8 --- I realize that is_a is deprecated, but it's more convenient in this situation. S