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'); 47var_dump($rp->isInitialized($a)); 48var_dump($rp->isInitialized($a)); 49 50echo "Object type:\n"; 51$rp = new ReflectionProperty('B', 'i'); 52var_dump($rp->isInitialized($a)); 53 54try { 55 var_dump($rp->isInitialized(new stdClass)); 56} catch (ReflectionException $e) { 57 echo $e->getMessage(), "\n"; 58} 59 60try { 61 var_dump($rp->isInitialized()); 62} catch (TypeError $e) { 63 echo $e->getMessage(), "\n"; 64} 65 66class WithMagic { 67 public $prop; 68 public int $intProp; 69 70 public function __isset($name) { 71 echo "__isset($name)\n"; 72 return true; 73 } 74 75 public function __get($name) { 76 echo "__get($name)\n"; 77 return "foobar"; 78 } 79} 80 81echo "Class with __isset:\n"; 82$obj = new WithMagic; 83unset($obj->prop); 84$rp = new ReflectionProperty('WithMagic', 'prop'); 85var_dump($rp->isInitialized($obj)); 86$rp = new ReflectionProperty('WithMagic', 'intProp'); 87var_dump($rp->isInitialized($obj)); 88 89?> 90--EXPECT-- 91Static properties: 92bool(true) 93bool(false) 94bool(true) 95Declared properties: 96bool(true) 97bool(false) 98bool(true) 99Declared properties after unset: 100bool(false) 101bool(false) 102bool(false) 103Dynamic properties: 104bool(true) 105bool(false) 106Visibility handling: 107bool(false) 108bool(false) 109Object type: 110bool(false) 111Given object is not an instance of the class this property was declared in 112ReflectionProperty::isInitialized(): Argument #1 ($object) must be provided for instance properties 113Class with __isset: 114bool(false) 115bool(false) 116