INSTANCEOF

PHP code

<?php
/*
 * 
 * opcode number: 138
 */
$obj = new A();

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

PHP opcodes

Function name: (null)

Compiled variables: !0=$obj

line#op fetchextreturn operands
60 ZEND_FETCH_CLASS   :0 'A'
 1 NEW   $1 :0
 2 DO_FCALL_BY_NAME  0   
 3 ASSIGN     !0,$1
84 ZEND_FETCH_CLASS   :4 'A'
 5 ZEND_INSTANCEOF   ~5 !0,$4
 6 JMPZ     ~5,->9
97 ECHO     'A'
108 JMP     ->9
119 RETURN     1
add a note add a note

User Contributed Notes 6 notes

up
41
cody at codysnider dot com
5 years ago
When checking instanceof against a subclass of the class in question, it will return true.

<?php

class Foo {

    public
$foobar = 'Foo';
   
    public function
test() {
        echo
$this->foobar . "\n";
    }

}

class
Bar extends Foo {

    public
$foobar = 'Bar';

}

$a = new Foo();
$b = new Bar();

echo
"use of test() method\n";
$a->test();
$b->test();

echo
"instanceof Foo\n";
var_dump($a instanceof Foo); // TRUE
var_dump($b instanceof Foo); // TRUE

echo "instanceof Bar\n";
var_dump($a instanceof Bar); // FALSE
var_dump($b instanceof Bar); // TRUE

echo "subclass of Foo\n";
var_dump(is_subclass_of($a, 'Foo')); // FALSE
var_dump(is_subclass_of($b, 'Foo')); // TRUE

echo "subclass of Bar\n";
var_dump(is_subclass_of($a, 'Bar')); // FALSE
var_dump(is_subclass_of($b, 'Bar')); // FALSE

?>

Result (CLI, 5.4.4):

use of test() method
Foo
Bar
instanceof Foo
bool(true)
bool(true)
instanceof Bar
bool(false)
bool(true)
subclass of Foo
bool(false)
bool(true)
subclass of Bar
bool(false)
bool(false)
up
10
portugal {at} jawira {dot} com
2 years ago
I'm commenting here because the note from "admin at torntech" is incomplete. You can perfectly replace "is_a()" function with "instanceof" operator. However you must use a variable to store the class name, otherwise you will get a Parse error:

<?php
$object
= new \stdClass();
$class_name = '\stdClass';

var_dump($object instanceof $class_name);    // bool(true)
var_dump($object instanceof '\stdClass');    // Parse error: syntax error, unexpected ''\stdClass'' (T_CONSTANT_ENCAPSED_STRING)
?>

Please go to Type Operators page for more details about "instanceof": http://php.net/manual/en/language.operators.type.php
up
8
krall dot eugene at gmail dot com
2 years ago
When checking instanceof against a class that implements the class in question, it will return true.

<?php

interface ExampleInterface
{
    public function
interfaceMethod();

}

class
ExampleClass implements ExampleInterface
{
    public function
interfaceMethod()
    {
        return
'Hello World!';
    }
}

$exampleInstance = new ExampleClass();

if(
$exampleInstance instanceof ExampleInterface)
    echo
'Yes, it is';
else
    echo
'No, it is not';

?>

The result:

Yes, it is
up
2
katrinaelaine6 at gmail dot com
7 months ago
instanceof also works on abstract classes and interfaces.

<?php

abstract class Grandpa {}
interface
Father {}
interface
Son {}

class
Base extends Grandpa implements Father {}
class
Child extends Base implements Son {}

$b = new Base();
$c = new Child();

/* All of the following are true. */

var_dump($b instanceof Grandpa);
var_dump($b instanceof Father);

var_dump($c instanceof Son);
var_dump($c instanceof Father);
var_dump($c instanceof Grandpa);

?>
up
13
asdf at asdf dot com
5 years ago
Please note, that you get no warnings on non-existent classes:

<?php
class A() {
}

$a = new A();

$exists = ($a instanceof A); //TRUE
$exists = ($a instanceof NonExistentClass); //FALSE
up
-2
admin at torntech dot com
2 years ago
Despite this section being opcode examples, for php 5.3+ see also is_a()
*Note php 5.0 - 5.2 is_a() was depreciated in favor of instanceof but was undepreciated in 5.3+.

Allows for string comparison against the class name and functions in the same manner as ">&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&ss="def6spanner as ">&&&&&&&&&&&&&&&R6anner as ">&&&6tep&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&Anst a al/en/i&&&&&&&ather
extends }(= new Foo;
(
var_dump(instanceof FooBar />(// bool(true. */
(
var_dump((, \\g">'Bar') />(// bool(true. */
(
var_dump(instanceof \\g">'Bar') />(?>
">
add a note add a note -2 his sec>> id="layout-con dif] Opcode Descriptions and Examples] ] <
    Ctmainmeof.'f] ] ]
  • ]
  • ]
  • ]
  • ]
  • ]
  • ]
  • AS" ASSIit
  • ]
  • AS_​ADDaultd>AS_​ADDSIit
  • ]
  • AS_​BW_​ANDaultd>AS_​BW_​ANDSIit
  • ]
  • AS_​BW_​ORaultd>AS_​BW_​OREdit
  • ]
  • AS_​BW_​XORaultd>AS_​BW_​XOREdit
  • ]
  • AS_​CONCATaultd>AS_​CONCATEdit
  • ]
  • AS_​DIMaultd>AS_​DIMEdit
  • ]
  • AS_​DIVaultd>AS_​DIVEdit
  • ]
  • AS_​MODaultd>AS_​MODEdit
  • ]
  • AS_​MULaultd>AS_​MULEdit
  • ]
  • AS_​OBJaultd>AS_​OBJEdit
  • ]
  • AS_​REFaultd>AS_​REFEdit
  • ]
  • AS_​SLaultd>AS_​SLEdit
  • ]
  • AS_​SRaultd>AS_​SREdit
  • ]
  • AS_​SUBaultd>AS_​SUBEdit
  • ]
  • ]
  • ]
  • ]
  • ]
  • ]
  • ]
  • ]
  • ]
  • ]
  • ]
  • ]
  • ]
  • ]
  • ]
  • ]
  • DECLARE_​CCH_CEdit
  • ]
  • DECLARE_​C(T_CEdit
  • ]
  • DECLARE_​FUNCTIONEdit
  • ]
  • DECLARE_​INHERITED_​CCH_CEdit
  • ]
  • DECLARE_​INHERITED_​CCH_C_​DELAYEDEdit
  • ]
  • ]
  • DO_​FDO_FEdit
  • ]
  • DO_​FDO_F_​BY_​_BY_Edit
  • ]
  • " ECit
  • ]
  • Z​SILENCEautd>Z​SILENCEECit
  • ]
  • EXICECit
  • ]
  • EXT_​FDO_F_​BEGISECit
  • ]
  • ]
  • ]
  • ]
  • ]
  • ]
  • END_FE​CCH_CECit
  • ]
  • END_FE​C(T_CONSECit
  • ]
  • END_FE​DIM_​FUNC_​ARGECit
  • ]
  • END_FE​DIM_​ICECit
  • ]
  • END_FE​DIM_​REdit
  • ]
  • END_FE​DIM_​RWEdit
  • ]
  • ]
  • ]
  • END_FE​DIM_​WEdit
  • ]
  • END_FE​FUNC_​ARGECit
  • ]
  • END_FE​ICECit
  • ]
  • END_FE​OBJE​FUNC_​ARGECit
  • ]
  • END_FE​OBJE​ICECit
  • ]
  • ]
  • END_FE​OBJE​RWECit
  • ]
  • ]
  • END_FE​OBJE​WECit
  • ]
  • ]
  • END_FE​RWECit
  • ]
  • ]
  • END_FE​WECit
  • ]
  • EREEECit
  • ]
  • GOTOECit
  • ]
  • HANDLE_​EXCEPTIONECit
  • ]
  • INCLUDE_​OR_​EVALECit
  • ]
  • INIT_​ARRAYECit
  • ]
  • INIT_​FDO_F_​BY_​_BY_ECit
  • ]
  • INIT_​METHOD_​CO_FECit
  • ]
  • INIT_​NS_​FDO_F_​BY_​_BY_ECit
  • ]
  • INIT_​STATIC_​METHOD_​CO_FECit
  • ]
  • ]
  • ]
  • IS_​EQUALECit
  • ]
  • IS_​IDENTICALECit
  • ]
  • IS_​NOT_​EQUALECit
  • ]
  • IS_​NOT_​IDENTICALECit
  • ]
  • IS_​SMO_FERECit
  • ]
  • IS_​SMO_FER_​OR_​EQUALECit
  • ]
  • ]
  • ]
  • ]
  • JMPECit
  • ]
  • JMPNZECit
  • ]
  • JMPNZ_​EXECit
  • ]
  • JMPZECit
  • ]
  • JMPZ_​EXECit
  • ]
  • JMPZNZECit
  • ]
  • ]
  • ]
  • NEWEdit
  • ]
  • ]
  • POST_​DECECit
  • ]
  • ]
  • POST_​INCECit
  • ]
  • ]
  • PRE_​DECECit
  • ]
  • ]
  • PRE_​INCECit
  • ]
  • ]
  • PRINSECit
  • ]
  • AS" QM_​Atd>ASECit
  • ]
  • ]
  • ]
  • RECV_​INICECit
  • ]
  • RETURNECit
  • ]
  • ]
  • Z​REFauStd>Z​REFECit
  • ]
  • Z​VAL">Std>Z​VALECit
  • ]
  • Z​VAR">Std>Z​VARECit
  • ]
  • Z​VAR_​NOZ​REFauStd>Z​VAR_​NOZ​REFECit
  • ]
  • SLECit
  • ]
  • SRECit
  • ]
  • ]
  • SWID_FE​FREEEdit
  • ]
  • THROWEdit
  • ]
  • TICKSEdit
  • ]
  • UNSET_​DIMEdit
  • ]
  • ]
  • UNSET_​VAREdit
  • ]
  • USER_​OPCODEEdit
  • ]
  • VERIFY_​ABSTRACT_​CCH_CEdit
  • ]
  • Z​DECLARE_​LAMBDAE​FUNCTION">Ztd>Z​DECLARE_​LAMBDAE​FUNCTIONEdit
  • ]
  • Z​JMP_​SETauZtd>Z​JMP_​SETEdit
  • ] ] Edi con> > Ed id="la dif] ] E">
    Ex="inteions thirdsp//ty librariect dif] bs/jquery/1.10.2/jquery.min.js"br / Desc> on"idittoTor.p>