1--TEST-- 2Test ReflectionProperty::isInitialized() 3--FILE-- 4<?php 5 6class A { 7 public static ?string $ssv = null; 8 public static ?string $ss; 9 public static $s; 10 public ?int $iv = null; 11 public ?int $i; 12 public $n; 13 14 private int $p; 15} 16 17class B extends A { } 18 19echo "Static properties:\n"; 20var_dump((new ReflectionProperty(A::class, 'ssv'))->isInitialized()); 21var_dump((new ReflectionProperty(A::class, 'ss'))->isInitialized()); 22var_dump((new ReflectionProperty(A::class, 's'))->isInitialized()); 23 24echo "Declared properties:\n"; 25$a = new A; 26var_dump((new ReflectionProperty($a, 'iv'))->isInitialized($a)); 27var_dump((new ReflectionProperty($a, 'i'))->isInitialized($a)); 28var_dump((new ReflectionProperty($a, 'n'))->isInitialized($a)); 29 30echo "Declared properties after unset:\n"; 31unset($a->iv); 32unset($a->i); 33unset($a->n); 34var_dump((new ReflectionProperty($a, 'i'))->isInitialized($a)); 35var_dump((new ReflectionProperty($a, 'iv'))->isInitialized($a)); 36var_dump((new ReflectionProperty($a, 'n'))->isInitialized($a)); 37 38echo "Dynamic properties:\n"; 39$a->d = null; 40$rp = new ReflectionProperty($a, 'd'); 41var_dump($rp->isInitialized($a)); 42unset($a->d); 43var_dump($rp->isInitialized($a)); 44 45echo "Visibility handling:\n"; 46$rp = new ReflectionProperty('A', 'p'); 47try { 48 var_dump($rp->isInitialized($a)); 49} catch (ReflectionException $e) { 50 echo $e->getMessage(), "\n"; 51} 52$rp->setAccessible(true); 53var_dump($rp->isInitialized($a)); 54 55echo "Object type:\n"; 56$rp = new ReflectionProperty('B', 'i'); 57var_dump($rp->isInitialized($a)); 58 59try { 60 var_dump($rp->isInitialized(new stdClass)); 61} catch (ReflectionException $e) { 62 echo $e->getMessage(), "\n"; 63} 64 65class WithMagic { 66 public $prop; 67 public int $intProp; 68 69 public function __isset($name) { 70 echo "__isset($name)\n"; 71 return true; 72 } 73 74 public function __get($name) { 75 echo "__get($name)\n"; 76 return "foobar"; 77 } 78} 79 80echo "Class with __isset:\n"; 81$obj = new WithMagic; 82unset($obj->prop); 83$rp = new ReflectionProperty('WithMagic', 'prop'); 84var_dump($rp->isInitialized($obj)); 85$rp = new ReflectionProperty('WithMagic', 'intProp'); 86var_dump($rp->isInitialized($obj)); 87 88?> 89--EXPECT-- 90Static properties: 91bool(true) 92bool(false) 93bool(true) 94Declared properties: 95bool(true) 96bool(false) 97bool(true) 98Declared properties after unset: 99bool(false) 100bool(false) 101bool(false) 102Dynamic properties: 103bool(true) 104bool(false) 105Visibility handling: 106Cannot access non-public member A::$p 107bool(false) 108Object type: 109bool(false) 110Given object is not an instance of the class this property was declared in 111Class with __isset: 112bool(false) 113bool(false) 114