2 Test is_object() function
5 /* Prototype: bool is_object ( mixed $var );
6 * Description: Finds whether the given variable is an object
9 echo "*** Testing is_object() with valid objects ***\n";
11 // class with no members
18 abstract class abstractClass
20 abstract protected function getClassName();
21 public function printClassName () {
22 echo $this->getClassName() . "\n";
26 // implement abstract class
27 class concreteClass extends abstractClass
29 protected function getClassName() {
30 return "concreteClass";
37 public function setVal ($name, $val);
38 public function dumpVal ();
41 // implement the interface
42 class Value implements IValue
44 private $vars = array ();
46 public function setVal ( $name, $val ) {
47 $this->vars[$name] = $val;
50 public function dumpVal () {
62 protected $protected_var;
64 function __construct ( ) {
65 $this->foo_object = new foo();
66 $this->public_var = 10;
67 $this->public_var1 = new foo();
68 $this->private_var = new foo();
69 $this->proected_var = new foo();
73 // create a object of each class defined above
74 $myClass_object = new myClass();
75 $foo_object = new foo();
76 $Value_object = new Value();
77 $concreteClass_object = new concreteClass();
79 $valid_objects = array(
86 $myClass_object->foo_object,
87 $myClass_object->public_var1,
93 /* loop to check that is_object() recognizes different
94 objects, expected output: bool(true) */
96 foreach ($valid_objects as $object ) {
97 echo "-- Iteration $loop_counter --\n"; $loop_counter++;
98 var_dump( is_object($object) );
101 echo "\n*** Testing is_object() on non object types ***\n";
103 // get a resource type variable
104 $fp = fopen (__FILE__, "r");
105 $dfp = opendir ( __DIR__ );
108 $unset_object = new foo();
109 unset ($unset_object);
111 // other types in a array
112 $not_objects = array (
116 -10.0000000000000000005,
130 @$unset_object, // unset object
131 @$undefined_var, // undefined variable
133 /* loop through the $not_objects to see working of
134 is_object() on non object types, expected output: bool(false) */
136 foreach ($not_objects as $type ) {
137 echo "-- Iteration $loop_counter --\n"; $loop_counter++;
138 var_dump( is_object($type) );
143 // close the resources used
149 *** Testing is_object() with valid objects ***
173 *** Testing is_object() on non object types ***