1
2<?php
3/*
4 *
5 * opcode number: 138
6 */
7$obj = new A();
8
9if ($obj instanceof A) {
10 echo 'A';
11}
12?>
13
14
1class MyClass {
2}
3
4$o1 = new MyClass();
5$o2 = new MyClass();
6$name = 'MyClass';
7
8// in the cases below, $a gets boolean value true
9$a = $o1 instanceof MyClass;
10$a = $o1 instanceof $name;
11$a = $o1 instanceof $o2;
12
13// counter examples:
14$b = 'b';
15$a = $o1 instanceof 'MyClass'; // parse error: constant not allowed
16$a = false instanceof MyClass; // fatal error: constant not allowed
17$a = $b instanceof MyClass; // false ($b is not an object)
18
1<?php
2class MyClass {}
3class AnotherClass extends MyClass{}
4$obj = new AnotherClass();
5
6if($obj instanceof AnotherClass) {
7 echo "The object is AnotherClass";
8}
9// The object is also an instance of the class it is derived from
10if($obj instanceof MyClass) {
11 echo "The object is MyClass<br>";
12}
13?>
1$myObject instanceof MyClass
2
3//usualy in if()
4if($myObject instanceof MyClass)
5{
6 //Do it
7}