Re: is_a() versus instanceof

From: Date: Sat, 19 Dec 2009 10:20:29 +0000
Subject: Re: is_a() versus instanceof
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-46474@lists.php.net to get a copy of this message
Johannes "Schlüter" wrote:
On Sat, 2009-12-19 at 01:42 +0100, Johannes Mueller wrote:
if($foo instanceof bar){ .. } // runs without any notification
instanceof is a language construct expecting a class identifier. It doesn't complain about on-existing classes as it would need to trigger the __autoloader which might be way too much.
and if(is_a($foo, bar)){ .. } // runs with an undefined constant bar notification
is_a() is a function expecting a string. And this warning is unrelated to the fact that the class "bar" doesn't exist, but due to the way PHP treats undefined constants.
Thank you for your answers, that is what I've expected. Unfortunately this is sometimes annoying when using namespaces, where you have to take care of the namespaces too. I do understand the performance impact of a more complex solution and my fault was to mix two different operations. I expected instanceof and is_a() to check if the class exists. My personal problem was, that I didn't recognized instanceof to treat the requested class as a normal string, even it is not enclosed by quotation marks. The only protective barrier in this case seems to be a skilled IDE, that helps you to avoid referencing non existant classes. Cheers Johannes

« previous php.internals (#46474) next »