3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ParentClass { protected readonly int $value; } trait Foo { protected readonly int $value; } class ChildClassWithoutTrait extends ParentClass { protected readonly int $value; } $reflector = new ReflectionClass(ChildClassWithoutTrait::class); $instance = $reflector->newInstanceWithoutConstructor(); var_dump( $reflector->hasProperty('value'), $prop = $reflector->getProperty('value') ); $prop->setValue($instance, 20); var_dump($instance); echo '--------------------------------------------------------------------------------------------------------', PHP_EOL; class ClassWithTraitAndNoParent { use Foo; } $reflector = new ReflectionClass(ClassWithTraitAndNoParent::class); $instance = $reflector->newInstanceWithoutConstructor(); var_dump( $reflector->hasProperty('value'), $prop = $reflector->getProperty('value') ); $prop->setValue($instance, 20); var_dump($instance); echo '--------------------------------------------------------------------------------------------------------', PHP_EOL; class ChildClassWithTrait extends ParentClass { use Foo; } $reflector = new ReflectionClass(ChildClassWithTrait::class); $instance = $reflector->newInstanceWithoutConstructor(); var_dump( $reflector->hasProperty('value'), $prop = $reflector->getProperty('value') ); $prop->setValue($instance, 20); var_dump($instance);
Output for git.master, git.master_jit
bool(true) object(ReflectionProperty)#3 (2) { ["name"]=> string(5) "value" ["class"]=> string(22) "ChildClassWithoutTrait" } object(ChildClassWithoutTrait)#2 (1) { ["value":protected]=> int(20) } -------------------------------------------------------------------------------------------------------- bool(true) object(ReflectionProperty)#2 (2) { ["name"]=> string(5) "value" ["class"]=> string(25) "ClassWithTraitAndNoParent" } object(ClassWithTraitAndNoParent)#1 (1) { ["value":protected]=> int(20) } -------------------------------------------------------------------------------------------------------- bool(true) object(ReflectionProperty)#1 (2) { ["name"]=> string(5) "value" ["class"]=> string(11) "ParentClass" } object(ChildClassWithTrait)#4 (1) { ["value":protected]=> int(20) }
Output for rfc.property-hooks
bool(true) object(ReflectionProperty)#3 (2) { ["name"]=> string(5) "value" ["class"]=> string(22) "ChildClassWithoutTrait" } object(ChildClassWithoutTrait)#2 (1) { ["value":protected]=> int(20) } -------------------------------------------------------------------------------------------------------- bool(true) object(ReflectionProperty)#2 (2) { ["name"]=> string(5) "value" ["class"]=> string(25) "ClassWithTraitAndNoParent" } object(ClassWithTraitAndNoParent)#1 (1) { ["value":protected]=> int(20) } -------------------------------------------------------------------------------------------------------- bool(true) object(ReflectionProperty)#1 (2) { ["name"]=> string(5) "value" ["class"]=> string(11) "ParentClass" } Fatal error: Uncaught Error: Cannot initialize readonly property ParentClass::$value from scope ChildClassWithTrait in /in/Zbnqa:64 Stack trace: #0 /in/Zbnqa(64): ReflectionProperty->setValue(Object(ChildClassWithTrait), 20) #1 {main} thrown in /in/Zbnqa on line 64
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
27.26 ms | 409 KiB | 5 Q