Hi,
is it intended to have different behaviours of is_a()
and instanceof?
Let's assume you have the following code snippets:
class foo{}
class baz{} //<-you have a typo here, want to write bar
if($foo instanceof bar){
..
}
// runs without any notification
and
if(is_a($foo, bar)){
..
}
// runs with an undefined constant bar notification
I think the instanceof solution can cause problems, because you can not
trigger the problem. What do you think?
Johannes
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.
Behavior is expected,
johannes
Johannes Schlüter schreef:
if($foo instanceof bar){
..
}
// runs without any notificationinstanceof 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.
Wouldn't it be an idea to make a setting for this ? I find it very
annoying (however understandable) it doesn't complain if I make a typo..
I assume this works the same as try-catch(Exceptio $e); (note the
missing 'n'); which got me fooled before.
(To be sure to be understood: Create a setting which does check the
class-existance; even when autoloading must be called for it)
-- Jille
Johannes "Schlüter" wrote:
if($foo instanceof bar){
..
}
// runs without any notificationinstanceof 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
class rootObject {
...
function isMemberOrInstanceOfClass($object) {
$className = is_object($object) ? get_class($object) : $object;
return ($this instanceof $className);
}
};
Johannes "Schlüter" wrote:
if($foo instanceof bar){
..
}
// runs without any notificationinstanceof 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 andis_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
Johannes Mueller wrote:
if(is_a($foo, bar)){
..
}
// runs with an undefined constant bar notificationI think the instanceof solution can cause problems, because you can not
trigger the problem. What do you think?
I think you meant:
if(is_a($foo, "bar")){
since is_a()
takes a string as the second parameter.
Does this correction then reset your expectation about instanceof?
Chris
--
Blog: http://blogs.oracle.com/opal
Twitter: http://twitter.com/ghrd