# is_a() vs. instanceof

- Thread: https://externals.io/message/12086
- Original archive: https://news-web.php.net/php.internals/12086
- Started by Hans Lellelid on 2004-08-11T00:36:45+00:00
- Messages: 7
- 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

---

## #12086 — Hans Lellelid — 2004-08-11T00:36:45+00:00

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

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

---

## #12098 — Dan Ostrowski — 2004-08-12T03:19:55+00:00

URL: https://externals.io/message/12086#12098 · In reply to: #12086

> 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

---

## #12099 — Alan Knowles — 2004-08-12T03:30:28+00:00

URL: https://externals.io/message/12086#12099 · In reply to: #12098

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:

>
>  
>
> > […]
>
>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

---

## #12100 — Sean Coates — 2004-08-12T03:37:30+00:00

URL: https://externals.io/message/12086#12100 · In reply to: #12098

Dan Ostrowski wrote:
> 
> > […]
*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

---

## #12101 — Hans Lellelid — 2004-08-12T03:52:16+00:00

URL: https://externals.io/message/12086#12101 · In reply to: #12099

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

---

## #12118 — Jevon Wright — 2004-08-12T09:51:15+00:00

URL: https://externals.io/message/12086#12118 · In reply to: #12115

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
>
> 
> > […]
pear):
> > […]
loaded.
> > […]

---

## #12115 — Al Baker — 2004-08-12T09:58:54+00:00

URL: https://externals.io/message/12086#12115 · In reply to: #12101

I agree, that's the exact behavior I would like as well and what most
people would expect.

Al


> Alan Knowles wrote:
> 
> > […]
> 
> 
> 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
