PHP instanceof Operator

15th October 2009 - 4 minutes read time

The instanceof operator is used in PHP to find out if an object is an instantiated instance of a class. It's quite easy to use and works in the same sort of way as other operators. This can be useful to controlling objects in large applications as you can make sure that a parameter is a particular instance of an object before using it. Lets create a couple of classes as examples.

  1. class Shape
  2. {
  3. }
  4.  
  5. class Circle extends Shape
  6. {
  7. }

Now, to find out if a variable is an instance of the Shape class create the object and then use the instanceof operator on the object. The following code returns true.

  1. $shape = new Shape();
  2. var_dump($shape instanceof Shape); // true

Notice that the Circle class above extends the Shape class. The instanceof operator will work with both an instance of the class and the parent class. This can lead to errors in your application if you aren't careful.

  1. $circle = new Circle();
  2.  
  3. var_dump($circle instanceof Shape); // true
  4. var_dump($circle instanceof Circle); // true

It is also possible to negate this operator to see if a variable is not an instance of an object. It is useful to see if a variable is not an instance of an operator and return false from a function as early as possible. Negating the operator will allow you to do this or even to throw an exception.

There are two legal ways to negate the instanceof operator. Either by enclosing the call in paranthses and then negating it like this:

var_dump(!($shape instanceof Shape)); // false

Or by simply adding the negating parameter to the variable like this:

var_dump(!$shape instanceof Shape); // false

With this code in hand it is now possible to do something like the following.

  1. function doObject($param)
  2. {
  3. if (!$param instanceof TheObject) {
  4. return false;
  5. }
  6. // rest of code here...
  7. }

There are other ways to use the instanceof operator, not just with the name of the class. You can use two other types of argument on the right hand side of the instanceof operator. The first is defining the classname as a string in a variable and passing this variable.

  1. $class = 'Circle';
  2. var_dump($circle instanceof $class); // true

The second is by passing an instance of the object itself. The following example creates two objects and essentially compares them with each other.

  1. $circle1 = 'Circle';
  2. $circle2 = 'Circle';
  3. var_dump($circle1 instanceof $circle2); // true

However, there are two really good ways to break this function and therefore your code. The first is by passing a string as the class name. This causes a parse error.

var_dump($circle instanceof 'Circle');

The second way is to pass an unset variable as the right hand side of the argument. This will cause a fatal error as the instanceof operator expects a valid object or a string.

var_dump($notandobject instanceof $notavariable);

Note that the operator is capable of accepting an unset variable on the left hand side, as long as the right hand side contains the all important string or object. This will (as you would expect) return false as the object isn't an instance of the class.

var_dump($notandobject instanceof $shape); // false

Comments

Permalink

Hi philip,
instanceof good explained. but got a case which is confusing me.

  1. class WishDB extends mysqli {
  2.  
  3. private static $instance = null;
  4.  
  5. public static function getInstance() {
  6. if (!self::$instance instanceof self) { // <<<<<<< what is this?
  7. self::$instance = new self; //// <<<<<<< and what is here self?
  8. }
  9. return self::$instance;
  10. }

 

Hubert (Thu, 11/17/2011 - 22:00)

Permalink

Good explain :)

Kauhsik (Mon, 12/17/2018 - 07:06)

Add new comment

The content of this field is kept private and will not be shown publicly.