]> granicus.if.org Git - php/blob
dd0e7689fd
[php] /
1 --TEST--
2 Test 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
9 echo "*** Testing is_object() with valid objects ***\n";
10
11 // class with no members
12 class foo
13 {
14 // no members
15 }
16
17 // abstract class
18 abstract class abstractClass
19 {
20   abstract protected function getClassName();
21   public function printClassName () {
22     echo $this->getClassName() . "\n";
23   }
24 }
25
26 // implement abstract class
27 class concreteClass extends abstractClass
28 {
29   protected function getClassName() {
30     return "concreteClass";
31   }
32 }
33
34 // interface class
35 interface IValue
36 {
37    public function setVal ($name, $val);
38    public function dumpVal ();
39 }
40
41 // implement the interface
42 class 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
56 class 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 __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();
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;
96 foreach ($valid_objects as $object ) {
97   echo "-- Iteration $loop_counter --\n"; $loop_counter++;
98   var_dump( is_object($object) );
99 }
100
101 echo "\n*** Testing is_object() on non object types ***\n";
102
103 // get a resource type variable
104 $fp = fopen (__FILE__, "r");
105 $dfp = opendir ( __DIR__ );
106
107 // unset object
108 $unset_object = new foo();
109 unset ($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;
136 foreach ($not_objects as $type ) {
137   echo "-- Iteration $loop_counter --\n"; $loop_counter++;
138   var_dump( is_object($type) );
139 }
140
141 echo "Done\n";
142
143 // close the resources used
144 fclose($fp);
145 closedir($dfp);
146
147 ?>
148 --EXPECTF--
149 *** Testing is_object() with valid objects ***
150 -- Iteration 1 --
151 bool(true)
152 -- Iteration 2 --
153 bool(true)
154 -- Iteration 3 --
155 bool(true)
156 -- Iteration 4 --
157 bool(true)
158 -- Iteration 5 --
159 bool(true)
160 -- Iteration 6 --
161 bool(true)
162 -- Iteration 7 --
163 bool(true)
164 -- Iteration 8 --
165 bool(true)
166 -- Iteration 9 --
167 bool(true)
168 -- Iteration 10 --
169 bool(true)
170 -- Iteration 11 --
171 bool(true)
172
173 *** Testing is_object() on non object types ***
174 -- Iteration 1 --
175 bool(false)
176 -- Iteration 2 --
177 bool(false)
178 -- Iteration 3 --
179 bool(false)
180 -- Iteration 4 --
181 bool(false)
182 -- Iteration 5 --
183 bool(false)
184 -- Iteration 6 --
185 bool(false)
186 -- Iteration 7 --
187 bool(false)
188 -- Iteration 8 --
189 bool(false)
190 -- Iteration 9 --
191 bool(false)
192 -- Iteration 10 --
193 bool(false)
194 -- Iteration 11 --
195 bool(false)
196 -- Iteration 12 --
197 bool(false)
198 -- Iteration 13 --
199 bool(false)
200 -- Iteration 14 --
201 bool(false)
202 -- Iteration 15 --
203 bool(false)
204 -- Iteration 16 --
205 bool(false)
206 -- Iteration 17 --
207 bool(false)
208 -- Iteration 18 --
209 bool(false)
210 -- Iteration 19 --
211 bool(false)
212 Done