search for in the  
<Array OperatorsControl Structures>
Last updated: Thu, 19 May 2005

Type Operators

PHP has a single type operator: instanceof. instanceof is used to determine whether a given object is of a specified object class.

The instanceof operator was introduced in PHP 5. Before this time is_a() was used but is_a() has since been deprecated in favor of instanceof.

<?php
class A { }
class B { }

$thing = new A;

if ($thing instanceof A) {
    echo 'A';
}
if ($thing instanceof B) {
    echo 'B';
}
?>

As $thing is an object of type A, but not B, only the block dependent on the A type will be executed:

A

See also get_class() and is_a().



User Contributed Notes
Type Operators
archanglmr at yahoo dot com
17-Feb-2005 08:37
Negated instanceof doesn't seem to be documented. When I read instanceof I think of it as a compairson operator (which I suppose it's not).

<?php
class A {}
class
X {}

//parse error from !
if (new X !instanceof A) {
   throw new
Exception('X is not an A');
}
//proper way to negate instanceof ?
if (!(new X instanceof A)) {
   throw new
Exception('X is not an A');
}
?>
d dot schneider at 24you dot de
18-Dec-2004 02:42
use this for cross-version development...

<?php

function is_instance_of($IIO_INSTANCE, $IIO_CLASS){
   if(
floor(phpversion()) > 4){
       if(
$IIO_INSTANCE instanceof $IIO_CLASS){
           return
true;
           }
       else{
           return
false;
           }
       }
   elseif(
floor(phpversion()) > 3){
       return
is_a($IIO_INSTANCE, $IIO_CLASS);
       }
   else{
       return
false;
       }
   }

?>
glen at arkadia-systems dot com
11-Sep-2004 12:00
If you are updating code to replace 'is_a()' with the PHP5 compliant
'instanceof' operator, you may find it necessary to change the basic
architecture of your code to make this work.

'instanceof' will throw a fatal error if the object being checked for does not
exist.

'is_a()' will simply return false under this condition.

In the example below, 'is_a()' would effectively detect the need to create an
instance of class 'A', but in this case 'instanceof' will crash your program
with a fatal error.

<?php
// define classes

// Commenting this class will cause an error when 'instanceof' looks for 'A'
// class A {}

class B {}

// create a new object
// $thing = new A();
$thing = new B();

if (
is_a($thing, 'A')) {
   echo
'Yes, $thing is_a A<br>';
} else {
   echo
'No, $thing is not is_a A<br>';
}

if (
$thing instanceof A) {
   echo
'Yes, $thing is an instaceof A<br>';
} else {
   echo
'No, $thing is not an instaceof A<br>';
}

?>

>>> Output:
No, $thing is not is_a A
Fatal error: Class 'A' not found in is_a-instanceof.php on line 19
>>>

A suitable work-around for this situation is to check for the existence of the
class before performing the 'instanceof' comparison:

<?php

/* ... */

if (class_exists('A') && $thing instanceof A) {
   echo
'Yes, $thing is an instaceof A<br>';
} else {
   echo
'No, $thing is not an instaceof A<br>';
}

?>
zimba dot spam at gmail dot com
10-Sep-2004 02:17
instanceof cannot use a string for the comparison like is_a.
Instead, you can use a class instance (not documented)
Example :
<?
$x
= new myClass();
$y = new myClass();

echo
is_a($x, get_class($y));  // Returns true/1

echo $x instanceof get_class($y);  // Doesn't work, instead use :

echo $x instanceof $y;
?>

Cheers,
zimba
"Leo Pedretti" <lpedretti at suserver dot com>
24-Aug-2004 10:59
The instanceof operator also checks the interface tree. For example, the following code:

<?

interface Human {
   public function
say($var);
   public function
gender();
}

class
man implements Human {
   public function
say($var) {
  
"Oh yeah, $var\n"
  
}

   public function
gender() {
   return
"male";
   }
}

class
woman implements Human {
   public function
say($var) {
  
"Oh dear, $var\n"
  
}

   public function
gender() {
   return
"female";
   }
}

$m = new man;
$w = new woman;
if (
$p instanceof Human) {
   print
"\$p is Human.\n";
} else {
   print
"\$p is not Human.\n";
}
print
"\$m is of gender ".$m->gender()."\n";

if (
$w instanceof Human) {
   print
"\$w is Human.\n";
} else {
   print
"\$w is not Human.\n";
}
print
"\$w is of gender ".$w->gender()."\n";
?>

Will produce the output

$m is Human.
$m is of gender male
$w is Human.
$w is of gender female
Jesse Scott (scotje at wwc dot edu)
06-Jul-2004 08:52
Since it's not stated authoritatively here, I'll add that instanceof *does* check all the way up the inheritence tree.

So in this code:

<?php
  
class A
  
{
       var
$someProp;
   }
  
   class
B extends A
  
{
       function
showProp()
       {
           echo
$someProp;
       }
   }
  
  
$obj1 = new B;

   if (
$obj1 instanceof A)
       echo
"Instanceof checks inheritence tree!";
   else
       echo
"Instanceof does not check inheritence tree!";
?>

The first branch of the if/else block is executed since ($obj1 instanceof A) evaulates to TRUE.

<Array OperatorsControl Structures>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT