Home > PHP, PHP Tutorials, PHP5 OOPS Tutorials > PHP5 Tutorial – instanceOf Operator Explained

PHP5 Tutorial – instanceOf Operator Explained

November 15th, 2007 admin Leave a comment Go to comments

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




Categories: PHP, PHP Tutorials, PHP5 OOPS Tutorials Tags:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. Stephan
    November 17th, 2007 at 17:47 | #1

    Just a little addendum:
    instanceof checks for interface inheritance too

  2. Bharath
    March 19th, 2010 at 13:15 | #2

    In the last example of inheritance you said :

    “However the reverse is not possible, we cannot compare if($p1 instanceof $c1) and will result in an error.”

    Why will it generate an error? Will it not go to else block if the condition fails? I mean since $p1 is not same not from same class or not inherited it should go to else block.

    Correct me if I am wrong.

    Thanks

  1. No trackbacks yet.
Enter this code to leave comment (Sorry, but bots get me crazy :) )