PHP5 Tutorial - instanceOf Operator Explained

PHP5 introduces a new operator by the name of instanceOf. instanceOf is used to check if two objects passed as operands belong to the same class. This check can also happen when an object is compared with a class name.

In PHP4 a similar functionality existed with a method is_a(), which has been replaced by the instanceOf operator in PHP5.






Lets look at an example below:

class Person {
	...
}
 
$p1 = new Person();
$p2 = new Person();
 
if($p1 instanceof $p2) 
    echo "True";
else
    echo "False";

In the above example, we are comparing to check if $p1 and $p2 belong to the same class i.e. Person. In this case $p1 and $p2 both belong to the same class Person and hence the output is True. Please note that line number 8 can also be written as if($p2 instanceof $p1) and will yield the same output.

You can also use instanceOf operator to compare an object with the name of the class, look at the example below:

class Person {
	...
}
 
$p1 = new Person();
 
if($p1 instanceof Person) 
    echo "True";
else
    echo "False";

In the above example, on line number 7 $p1 object is being compared to check if its a Person type of object. In this case $p1 is a Person type of object and hence the output is True.

Behaviour of instanceOf operator in inheritance

class Customer extends Person {
	...
}
 
$p1 = new Person();
$c1 = new Customer();
 
if($c1 instanceof $p1) 
    echo "True";
else
    echo "False";

In the above example, on line number 8 $c1 child class object is being compared with $p1 which is a parent class object. This is possible because Customer class is a child of the Person Class, just that the Customer class is a specialized form of the Person class and therefore this becomes possible. However the reverse is not possible, we cannot compare if($p1 instanceof $c1) and will result in an error.

Related Posts on PHP5 Tutorial - Object Oriented Programming (OOPS)

  1. PHP5 Tutorial - Learn to create a PHP5 Class
  2. PHP5 Tutorial - Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial - Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial - Defining Methods of a PHP5 Class
  5. PHP5 Tutorial - Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected
  8. PHP5 Tutorial - $this variable explained
  9. PHP5 Tutorial - instanceOf Operator Explained
  10. PHP5 Tutorial - Magic Methods - __toString() method
  11. PHP5 Tutorial - Magic Methods - __get() and __set()
  12. PHP5 Tutorial - Magic Methods - __isset() and __unset()
  13. PHP5 Tutorial - Magic Methods - __call() method
  14. PHP5 Tutorial - Magic Methods - __autoload() method
  15. PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()
  16. PHP5 Tutorial - Magic Methods - __clone() method




del.icio.us Reddit Slashdot Digg Facebook Technorati Google StumbleUpon Windows Live Furl Netscape Yahoo Bloglines Bookmark.it Ask Spurl Diigo

One Response to “PHP5 Tutorial - instanceOf Operator Explained”

  1. Just a little addendum:
    instanceof checks for interface inheritance too

Leave a Reply

Top Internet blogs Programming Blogs - Blog Catalog Blog Directory TopOfBlogs Technology blogs Top Blog Topsites List