1--TEST-- 2ReflectionClass::getProperty() 3--CREDITS-- 4Robin Fernandes <robinf@php.net> 5Steve Seear <stevseea@php.net> 6--FILE-- 7<?php 8class A { 9 static public $pubC = "pubC in A"; 10 static protected $protC = "protC in A"; 11 static private $privC = "privC in A"; 12 13 static public $pubA = "pubA in A"; 14 static protected $protA = "protA in A"; 15 static private $privA = "privA in A"; 16} 17 18class B extends A { 19 static public $pubC = "pubC in B"; 20 static protected $protC = "protC in B"; 21 static private $privC = "privC in B"; 22 23 static public $pubB = "pubB in B"; 24 static protected $protB = "protB in B"; 25 static private $privB = "privB in B"; 26} 27 28class C extends B { 29 static public $pubC = "pubC in C"; 30 static protected $protC = "protC in C"; 31 static private $privC = "privC in C"; 32} 33 34class X { 35 static public $pubC = "pubC in X"; 36 static protected $protC = "protC in X"; 37 static private $privC = "privC in X"; 38} 39 40$myC = new C; 41$rc = new ReflectionClass("C"); 42 43function showInfo($name) { 44 global $rc, $myC; 45 echo "--- (Reflecting on $name) ---\n"; 46 try { 47 $rp = $rc->getProperty($name); 48 } catch (Exception $e) { 49 echo $e->getMessage() . "\n"; 50 return; 51 } 52 try { 53 var_dump($rp); 54 var_dump($rp->getValue($myC)); 55 } catch (Exception $e) { 56 echo $e->getMessage() . "\n"; 57 return; 58 } 59} 60 61 62showInfo("pubA"); 63showInfo("protA"); 64showInfo("privA"); 65 66showInfo("pubB"); 67showInfo("protB"); 68showInfo("privB"); 69 70showInfo("pubC"); 71showInfo("protC"); 72showInfo("privC"); 73showInfo("doesNotExist"); 74 75showInfo("A::pubC"); 76showInfo("A::protC"); 77showInfo("A::privC"); 78 79showInfo("B::pubC"); 80showInfo("B::protC"); 81showInfo("B::privC"); 82 83showInfo("c::pubC"); 84showInfo("c::PUBC"); 85showInfo("C::pubC"); 86showInfo("C::protC"); 87showInfo("C::privC"); 88 89showInfo("X::pubC"); 90showInfo("X::protC"); 91showInfo("X::privC"); 92showInfo("X::doesNotExist"); 93 94showInfo("doesNotexist::doesNotExist"); 95 96?> 97--EXPECTF-- 98--- (Reflecting on pubA) --- 99object(ReflectionProperty)#%d (2) { 100 ["name"]=> 101 string(4) "pubA" 102 ["class"]=> 103 string(1) "A" 104} 105string(9) "pubA in A" 106--- (Reflecting on protA) --- 107object(ReflectionProperty)#%d (2) { 108 ["name"]=> 109 string(5) "protA" 110 ["class"]=> 111 string(1) "A" 112} 113Cannot access non-public property C::$protA 114--- (Reflecting on privA) --- 115Property C::$privA does not exist 116--- (Reflecting on pubB) --- 117object(ReflectionProperty)#%d (2) { 118 ["name"]=> 119 string(4) "pubB" 120 ["class"]=> 121 string(1) "B" 122} 123string(9) "pubB in B" 124--- (Reflecting on protB) --- 125object(ReflectionProperty)#%d (2) { 126 ["name"]=> 127 string(5) "protB" 128 ["class"]=> 129 string(1) "B" 130} 131Cannot access non-public property C::$protB 132--- (Reflecting on privB) --- 133Property C::$privB does not exist 134--- (Reflecting on pubC) --- 135object(ReflectionProperty)#%d (2) { 136 ["name"]=> 137 string(4) "pubC" 138 ["class"]=> 139 string(1) "C" 140} 141string(9) "pubC in C" 142--- (Reflecting on protC) --- 143object(ReflectionProperty)#%d (2) { 144 ["name"]=> 145 string(5) "protC" 146 ["class"]=> 147 string(1) "C" 148} 149Cannot access non-public property C::$protC 150--- (Reflecting on privC) --- 151object(ReflectionProperty)#%d (2) { 152 ["name"]=> 153 string(5) "privC" 154 ["class"]=> 155 string(1) "C" 156} 157Cannot access non-public property C::$privC 158--- (Reflecting on doesNotExist) --- 159Property C::$doesNotExist does not exist 160--- (Reflecting on A::pubC) --- 161object(ReflectionProperty)#%d (2) { 162 ["name"]=> 163 string(4) "pubC" 164 ["class"]=> 165 string(1) "A" 166} 167string(9) "pubC in A" 168--- (Reflecting on A::protC) --- 169object(ReflectionProperty)#%d (2) { 170 ["name"]=> 171 string(5) "protC" 172 ["class"]=> 173 string(1) "A" 174} 175Cannot access non-public property A::$protC 176--- (Reflecting on A::privC) --- 177object(ReflectionProperty)#%d (2) { 178 ["name"]=> 179 string(5) "privC" 180 ["class"]=> 181 string(1) "A" 182} 183Cannot access non-public property A::$privC 184--- (Reflecting on B::pubC) --- 185object(ReflectionProperty)#%d (2) { 186 ["name"]=> 187 string(4) "pubC" 188 ["class"]=> 189 string(1) "B" 190} 191string(9) "pubC in B" 192--- (Reflecting on B::protC) --- 193object(ReflectionProperty)#%d (2) { 194 ["name"]=> 195 string(5) "protC" 196 ["class"]=> 197 string(1) "B" 198} 199Cannot access non-public property B::$protC 200--- (Reflecting on B::privC) --- 201object(ReflectionProperty)#%d (2) { 202 ["name"]=> 203 string(5) "privC" 204 ["class"]=> 205 string(1) "B" 206} 207Cannot access non-public property B::$privC 208--- (Reflecting on c::pubC) --- 209object(ReflectionProperty)#%d (2) { 210 ["name"]=> 211 string(4) "pubC" 212 ["class"]=> 213 string(1) "C" 214} 215string(9) "pubC in C" 216--- (Reflecting on c::PUBC) --- 217Property C::$PUBC does not exist 218--- (Reflecting on C::pubC) --- 219object(ReflectionProperty)#%d (2) { 220 ["name"]=> 221 string(4) "pubC" 222 ["class"]=> 223 string(1) "C" 224} 225string(9) "pubC in C" 226--- (Reflecting on C::protC) --- 227object(ReflectionProperty)#%d (2) { 228 ["name"]=> 229 string(5) "protC" 230 ["class"]=> 231 string(1) "C" 232} 233Cannot access non-public property C::$protC 234--- (Reflecting on C::privC) --- 235object(ReflectionProperty)#%d (2) { 236 ["name"]=> 237 string(5) "privC" 238 ["class"]=> 239 string(1) "C" 240} 241Cannot access non-public property C::$privC 242--- (Reflecting on X::pubC) --- 243Fully qualified property name X::$pubC does not specify a base class of C 244--- (Reflecting on X::protC) --- 245Fully qualified property name X::$protC does not specify a base class of C 246--- (Reflecting on X::privC) --- 247Fully qualified property name X::$privC does not specify a base class of C 248--- (Reflecting on X::doesNotExist) --- 249Fully qualified property name X::$doesNotExist does not specify a base class of C 250--- (Reflecting on doesNotexist::doesNotExist) --- 251Class "doesnotexist" does not exist 252