Featured Posts

Writing Website Scrapers in PHPWriting Website Scrapers in PHP This article discusses about how to write a website scraper using PHP for web site data extraction. The concepts taught can be applied and programmed in Java, C#, etc. Basically any language that has a...

Readmore

12 common programming mistakes to avoid12 common programming mistakes to avoid Programming is an art and science and like all art and science the only way to learn is from mistakes. I have made many... and I would like to share with you the mistakes that I have made over my journey...

Readmore

7 habits of highly effective freelance programmers7 habits of highly effective freelance programmers I have developed these based on my freelancing experience. Though I have discontinued freelancing, but would like to share my practices with you. These are basic practices and have been developed over...

Readmore

  • Prev
  • Next

PHP5 Tutorial - instanceOf Operator Explained

Posted on : 15-11-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 OOPS Tutorials

1

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




Comments (1)

Just a little addendum:
instanceof checks for interface inheritance too

Write a comment

Enter this code