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
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 | fetch | ext | return | operands |
|---|---|---|---|---|---|---|
| 6 | 0 | ZEND_FETCH_CLASS | :0 | 'A' | ||
| 1 | NEW | $1 | :0 | |||
| 2 | DO_FCALL_BY_NAME | 0 | ||||
| 3 | ASSIGN | !0,$1 | ||||
| 8 | 4 | ZEND_FETCH_CLASS | :4 | 'A' | ||
| 5 | ZEND_INSTANCEOF | ~5 | !0,$4 | |||
| 6 | JMPZ | ~5,->9 | ||||
| 9 | 7 | ECHO | 'A' | |||
| 10 | 8 | JMP | ->9 | |||
| 11 | 9 | RETURN | 1 |
asdf at asdf dot com
03-Apr-2012 10:13
Sasanapuri Satish
13-Jul-2011 01:46
The operator instanceof can also be used to determine whether an object of a class is inheriting property from another class or not.
Example:
<?php
class test
{ var $var;
function disp()
{
echo "Inside the class";
}
}
$obj=new test;
var_dump($obj instanceof test); // Prints bool(true)
?>
