# is_a() versus instanceof

- Thread: https://externals.io/message/46470
- Original archive: https://news-web.php.net/php.internals/46470
- Started by Johannes Mueller on 2009-12-19T00:42:00+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #46470 — Johannes Mueller — 2009-12-19T00:42:00+00:00

URL: https://externals.io/message/46470#46470

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

---

## #46471 — johannes@php.net — 2009-12-19T00:52:51+00:00

URL: https://externals.io/message/46470#46471 · In reply to: #46470

> 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

---

## #46472 — Christopher Jones — 2009-12-19T00:59:58+00:00

URL: https://externals.io/message/46470#46472 · In reply to: #46470

Johannes Mueller wrote:

> 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?

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

---

## #46473 — Jille Timmermans — 2009-12-19T01:41:41+00:00

URL: https://externals.io/message/46470#46473 · In reply to: #46471

Johannes Schlüter schreef:
> 
>   
> > […]
>
> 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.
>   
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

---

## #46474 — Johannes Mueller — 2009-12-19T10:20:29+00:00

URL: https://externals.io/message/46470#46474 · In reply to: #46471

Johannes "Schlüter" wrote:
> 
> > […]
>
> 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.
>
> > […]
>
> `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

---

## #46475 — mm w — 2009-12-19T19:29:09+00:00

URL: https://externals.io/message/46470#46475 · In reply to: #46474

class rootObject {
...
        function isMemberOrInstanceOfClass($object) {
                $className = is_object($object) ? get_class($object) : $object;
                return ($this instanceof $className);
        }
};


> Johannes "Schlüter" wrote:
> > […]
>
> 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
