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 |
- PHP Руководство
- Функции по категориям
- Индекс функций
- Ядро PHP: Руководство хакера
- Zend Engine 2 Opcodes
- ADD
- ADD_ARRAY_ELEMENT
- ADD_CHAR
- ADD_INTERFACE
- ADD_STRING
- ADD_VAR
- ASSIGN
- ASSIGN_ADD
- ASSIGN_BW_AND
- ASSIGN_BW_OR
- ASSIGN_BW_XOR
- ASSIGN_CONCAT
- ASSIGN_DIM
- ASSIGN_DIV
- ASSIGN_MOD
- ASSIGN_MUL
- ASSIGN_OBJ
- ASSIGN_REF
- ASSIGN_SL
- ASSIGN_SR
- ASSIGN_SUB
- BEGIN_SILENCE
- BOOL
- BOOL_NOT
- BOOL_XOR
- BRK
- BW_AND
- BW_NOT
- BW_OR
- BW_XOR
- CASE
- CAST
- CATCH
- CLONE
- CONCAT
- CONT
- DECLARE_CLASS
- DECLARE_CONST
- DECLARE_FUNCTION
- DECLARE_INHERITED_CLASS
- DECLARE_INHERITED_CLASS_DELAYED
- DIV
- DO_FCALL
- DO_FCALL_BY_NAME
- ECHO
- END_SILENCE
- EXIT
- EXT_FCALL_BEGIN
- EXT_FCALL_END
- EXT_NOP
- EXT_STMT
- FE_FETCH
- FE_RESET
- FETCH_CLASS
- FETCH_CONSTANT
- FETCH_DIM_FUNC_ARG
- FETCH_DIM_IS
- FETCH_DIM_R
- FETCH_DIM_RW
- FETCH_DIM_TMP_VAR
- FETCH_DIM_UNSET
- FETCH_DIM_W
- FETCH_FUNC_ARG
- FETCH_IS
- FETCH_OBJ_FUNC_ARG
- FETCH_OBJ_IS
- FETCH_OBJ_R
- FETCH_OBJ_RW
- FETCH_OBJ_UNSET
- FETCH_OBJ_W
- FETCH_R
- FETCH_RW
- FETCH_UNSET
- FETCH_W
- FREE
- GOTO
- HANDLE_EXCEPTION
- INCLUDE_OR_EVAL
- INIT_ARRAY
- INIT_FCALL_BY_NAME
- INIT_METHOD_CALL
- INIT_NS_FCALL_BY_NAME
- INIT_STATIC_METHOD_CALL
- INIT_STRING
- INSTANCEOF
- IS_EQUAL
- IS_IDENTICAL
- IS_NOT_EQUAL
- IS_NOT_IDENTICAL
- IS_SMALLER
- IS_SMALLER_OR_EQUAL
- ISSET_ISEMPTY_DIM_OBJ
- ISSET_ISEMPTY_PROP_OBJ
- ISSET_ISEMPTY_VAR
- JMP
- JMPNZ
- JMPNZ_EX
- JMPZ
- JMPZ_EX
- JMPZNZ
- MOD
- MUL
- NEW
- NOP
- POST_DEC
- POST_DEC_OBJ
- POST_INC
- POST_INC_OBJ
- PRE_DEC
- PRE_DEC_OBJ
- PRE_INC
- PRE_INC_OBJ
- QM_ASSIGN
- RAISE_ABSTRACT_ERROR
- RECV
- RECV_INIT
- RETURN
- RETURN_BY_REF
- SEND_REF
- SEND_VAL
- SEND_VAR
- SEND_VAR_NO_REF
- SL
- SR
- SUB
- SWITCH_FREE
- THROW
- TICKS
- UNSET_DIM
- UNSET_OBJ
- UNSET_VAR
- USER_OPCODE
- VERIFY_ABSTRACT_CLASS
- ZEND_DECLARE_LAMBDA_FUNCTION
- ZEND_JMP_SET
Коментарии
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
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)
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 cody mentions like so.
<?php
namespace Foo{
class Bar {}
class Baz extends Bar{}
}
namespace {
$baz = new FooBaz;
var_dump($baz instanceof FooBar);
/* returns bool(true) */
var_dump(is_a($baz, 'Foo\\Bar'));
/* returns bool(true) */
var_dump($baz instanceof 'Foo\\Bar');
/* returns Runtime parse error */
}
?>
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
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": language.operators.type
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);
?>