<?php
class Foo
{
private $a;
private $b;
public function __construct()
{
echo 'DA';
}
public function getA() {return $this->a;}
public function getB() {return $this->b;}
public function setA($a) {$this->a = $a;}
public function setB($b) {$this->b = $b;}
}
$class = new ReflectionClass('Foo');
$instance = $class->newInstanceWithoutConstructor();
var_dump($instance);
$reflect = new ReflectionObject($instance);
$properties = $reflect->getProperties();
$i = 0;
foreach ($properties as $property)
{
$property->setValue($i);
$i++;
}
var_dump($instance);
object(Foo)#2 (2) {
["a":"Foo":private]=>
NULL
["b":"Foo":private]=>
NULL
}
Fatal error: Uncaught ArgumentCountError: ReflectionProperty::setValue() expects exactly 2 arguments, 1 given in /in/80XWD:32
Stack trace:
#0 /in/80XWD(32): ReflectionProperty->setValue(0)
#1 {main}
thrown in /in/80XWD on line 32
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
object(Foo)#2 (2) {
["a":"Foo":private]=>
NULL
["b":"Foo":private]=>
NULL
}
Fatal error: Uncaught ArgumentCountError: ReflectionProperty::setValue() expects exactly 2 arguments, 1 given in /in/80XWD:32
Stack trace:
#0 /in/80XWD(32): ReflectionProperty->setValue(0)
#1 {main}
thrown in /in/80XWD on line 32
Process exited with code 255.
Output for 8.0.0 - 8.0.30
object(Foo)#2 (2) {
["a":"Foo":private]=>
NULL
["b":"Foo":private]=>
NULL
}
Fatal error: Uncaught ReflectionException: Cannot access non-public property Foo::$a in /in/80XWD:32
Stack trace:
#0 /in/80XWD(32): ReflectionProperty->setValue(0)
#1 {main}
thrown in /in/80XWD on line 32
Process exited with code 255.
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
object(Foo)#2 (2) {
["a":"Foo":private]=>
NULL
["b":"Foo":private]=>
NULL
}
Fatal error: Uncaught ReflectionException: Cannot access non-public member Foo::$a in /in/80XWD:32
Stack trace:
#0 /in/80XWD(32): ReflectionProperty->setValue(0)
#1 {main}
thrown in /in/80XWD on line 32
Process exited with code 255.
object(Foo)#2 (2) {
["a":"Foo":private]=>
NULL
["b":"Foo":private]=>
NULL
}
Fatal error: Uncaught ReflectionException: Cannot access non-public member Foo::a in /in/80XWD:32
Stack trace:
#0 /in/80XWD(32): ReflectionProperty->setValue(0)
#1 {main}
thrown in /in/80XWD on line 32
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.38
object(Foo)#2 (2) {
["a":"Foo":private]=>
NULL
["b":"Foo":private]=>
NULL
}
Fatal error: Uncaught exception 'ReflectionException' with message 'Cannot access non-public member Foo::a' in /in/80XWD:32
Stack trace:
#0 /in/80XWD(32): ReflectionProperty->setValue(0)
#1 {main}
thrown in /in/80XWD on line 32
Process exited with code 255.