Hudzilla.org - the homepage of Paul Hudson
Contents > Objects Wish List | Report Bug | About Me ]

6.8     Object type information: instanceof and is_subclass_of()

This is NOT the latest copy of this book; click here for the latest version.

bool is_subclass_of ( object object, string class_name)

Author's Note: Very new versions of PHP 5 (after 5.0.2) will allow you to specify a string as parameter one, and check whether the class named in that string is a subclass of parameter two.

As you can see, inheriting from class to class is an incredibly powerful way to build up functionality in your scripts. However, very often it is easy to get lost with your inheritance - how can you tell what class a given object is?

PHP comes to the rescue with a special keyword, "instanceof", which can be used like an operator. Instance of will return true if the object on the left-hand side is of the same class or a descendant of the class given on the right-hand side. For example, given the code $poppy = new poodle;:

if ($poppy instanceof poodle) { }
if (
$poppy instanceof dog) { }

Both of those if statements would evaluate to be true, because $poppy is an object of the poodle class, and also is a descendant of the dog class.

Author's Note: Java programmers will be happy to know that instanceof is the same old friend they've grown used to over the years. It is a great keyword to keep to hand, as any Java veteran will tell you, and you will almost certainly find yourself using it quite often in your scripts.

If you only want to know whether an object is a descendant of a class, and not of that class itself, you can use the is_subclass_of() function. This takes an object as its first parameter, a class name string as its second parameter, and returns either true or false depending on whether the first parameter is descended from the class specified in the second parameter.

Understanding the difference between instanceof and is_subclass_of() is crucial - this script should make it clear:

<?php
    
class dog { }
    class
poodle extends dog { }
    
$poppy = new poodle();
    print (int)(
$poppy instanceof poodle);
    print
"\n";
    print (int)
is_subclass_of($poppy, "poodle");
?>

That should output a 1 then a 0. Typecasting to int is used because boolean false is printed out as "" (blank), but by typecasting to an integer this becomes 0. As you can see, using instanceof reports true that $poppy is either a poodle or a dog, whereas is_subclass_of() reports false because $poppy is not descended from the class "poodle" - it is a poodle.

Author's Note: You can also use the instanceof keyword to see whether an object implements an interface.





<< 6.7.6 Iterating through object variables   6.9 Class type hints >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 06 Sep 2008

A lot, yes. But it is good practice for an array of Objects. You need to cast the Object to the specific class in order to get the function of that object.

A Java Programmer - 06 Sep 2008

Actually, using instanceof a lot is not considered good practice. Asking for the type means that there's something wrong with your abstraction. If you find you're coding things like "If you're this subtype, do this, if you're that subtype, do that, if you're..." then you should take those statements and move them to the classes you're checking. Polymorphism should be the one to decide a "path" based on subtype.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is three plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow