PHP instanceof Operator

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.

class Shape
{
}

class Circle extends Shape
{
}

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.

$shape = new Shape();
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.

$circle = new Circle();

var_dump($circle instanceof Shape); // true
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.

function doObject($param)
{
 if (!$param instanceof TheObject) {
  return false;
 }
 // rest of code here...
}

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.

$class = 'Circle';
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.

$circle1 = 'Circle';
$circle2 = 'Circle';
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

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

class WishDB extends mysqli {

private static $instance = null;

public static function getInstance() {
   if (!self::$instance instanceof self) { // <<<<<<< what is this?
     self::$instance = new self; //// <<<<<<< and what is here self?
   }
   return self::$instance;
}

 

Permalink

Good explain :)

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
6 + 7 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.