©
本文檔使用 php中文網手册 發布
<?php
$obj = new A ();
if ( $obj instanceof A ) {
echo 'A' ;
}
?>
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 |
[#1] portugal {at} jawira {dot} com [2015-06-17 20:49:15]
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
[#2] krall dot eugene at gmail dot com [2015-03-10 21:54:20]
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
[#3] admin at torntech dot com [2015-02-10 18:46:37]
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 Foo\Baz;
var_dump($baz instanceof Foo\Bar);
var_dump(is_a($baz, 'Foo\\Bar'));
var_dump($baz instanceof 'Foo\\Bar');
}
?>
[#4] cody at codysnider dot com [2012-06-20 18:54:51]
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)
[#5] asdf at asdf dot com [2012-04-03 22:13:47]
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