<?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);
You have javascript disabled. You will not be able to edit any code.