1--TEST-- 2Test is_object() function 3--FILE-- 4<?php 5/* Prototype: bool is_object ( mixed $var ); 6 * Description: Finds whether the given variable is an object 7 */ 8 9echo "*** Testing is_object() with valid objects ***\n"; 10 11// class with no members 12class foo 13{ 14// no members 15} 16 17// abstract class 18abstract class abstractClass 19{ 20 abstract protected function getClassName(); 21 public function printClassName () { 22 echo $this->getClassName() . "\n"; 23 } 24} 25 26// implement abstract class 27class concreteClass extends abstractClass 28{ 29 protected function getClassName() { 30 return "concreteClass"; 31 } 32} 33 34// interface class 35interface IValue 36{ 37 public function setVal ($name, $val); 38 public function dumpVal (); 39} 40 41// implement the interface 42class Value implements IValue 43{ 44 private $vars = array (); 45 46 public function setVal ( $name, $val ) { 47 $this->vars[$name] = $val; 48 } 49 50 public function dumpVal () { 51 var_dump ( $vars ); 52 } 53} 54 55// a gereral class 56class myClass 57{ 58 var $foo_object; 59 public $public_var; 60 public $public_var1; 61 private $private_var; 62 protected $protected_var; 63 64 function myClass ( ) { 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(); 70 } 71} 72 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(); 78 79$valid_objects = array( 80 new stdclass, 81 new foo, 82 new concreteClass, 83 new Value, 84 new myClass, 85 $myClass_object, 86 $myClass_object->foo_object, 87 $myClass_object->public_var1, 88 $foo_object, 89 $Value_object, 90 $concreteClass_object 91); 92 93/* loop to check that is_object() recognizes different 94 objects, expected output: bool(true) */ 95$loop_counter = 1; 96foreach ($valid_objects as $object ) { 97 echo "-- Iteration $loop_counter --\n"; $loop_counter++; 98 var_dump( is_object($object) ); 99} 100 101echo "\n*** Testing is_object() on non object types ***\n"; 102 103// get a resource type variable 104$fp = fopen (__FILE__, "r"); 105$dfp = opendir ( dirname(__FILE__) ); 106 107// unset object 108$unset_object = new foo(); 109unset ($unset_object); 110 111// other types in a array 112$not_objects = array ( 113 0, 114 -1, 115 0.1, 116 -10.0000000000000000005, 117 10.5e+5, 118 0xFF, 119 0123, 120 $fp, // resource 121 $dfp, 122 array(), 123 array("string"), 124 "0", 125 "1", 126 "", 127 true, 128 NULL, 129 null, 130 @$unset_object, // unset object 131 @$undefined_var, // undefined variable 132); 133/* loop through the $not_objects to see working of 134 is_object() on non object types, expected output: bool(false) */ 135$loop_counter = 1; 136foreach ($not_objects as $type ) { 137 echo "-- Iteration $loop_counter --\n"; $loop_counter++; 138 var_dump( is_object($type) ); 139} 140 141echo "\n*** Testing error conditions ***\n"; 142//Zero argument 143var_dump( is_object() ); 144 145//arguments more than expected 146var_dump( is_object($myClass_object, $myClass_object) ); 147 148echo "Done\n"; 149 150// close the resources used 151fclose($fp); 152closedir($dfp); 153 154?> 155--EXPECTF-- 156*** Testing is_object() with valid objects *** 157-- Iteration 1 -- 158bool(true) 159-- Iteration 2 -- 160bool(true) 161-- Iteration 3 -- 162bool(true) 163-- Iteration 4 -- 164bool(true) 165-- Iteration 5 -- 166bool(true) 167-- Iteration 6 -- 168bool(true) 169-- Iteration 7 -- 170bool(true) 171-- Iteration 8 -- 172bool(true) 173-- Iteration 9 -- 174bool(true) 175-- Iteration 10 -- 176bool(true) 177-- Iteration 11 -- 178bool(true) 179 180*** Testing is_object() on non object types *** 181-- Iteration 1 -- 182bool(false) 183-- Iteration 2 -- 184bool(false) 185-- Iteration 3 -- 186bool(false) 187-- Iteration 4 -- 188bool(false) 189-- Iteration 5 -- 190bool(false) 191-- Iteration 6 -- 192bool(false) 193-- Iteration 7 -- 194bool(false) 195-- Iteration 8 -- 196bool(false) 197-- Iteration 9 -- 198bool(false) 199-- Iteration 10 -- 200bool(false) 201-- Iteration 11 -- 202bool(false) 203-- Iteration 12 -- 204bool(false) 205-- Iteration 13 -- 206bool(false) 207-- Iteration 14 -- 208bool(false) 209-- Iteration 15 -- 210bool(false) 211-- Iteration 16 -- 212bool(false) 213-- Iteration 17 -- 214bool(false) 215-- Iteration 18 -- 216bool(false) 217-- Iteration 19 -- 218bool(false) 219 220*** Testing error conditions *** 221 222Warning: is_object() expects exactly 1 parameter, 0 given in %s on line %d 223bool(false) 224 225Warning: is_object() expects exactly 1 parameter, 2 given in %s on line %d 226bool(false) 227Done 228