3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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->setAccessible(true); $property->setValue($instance, $i); $i++; } var_dump($instance); $instance->a = 'Test'; var_dump($instance);
based on uKKJL
Output for 7.0.0 - 7.2.0
object(Foo)#2 (2) { ["a":"Foo":private]=> NULL ["b":"Foo":private]=> NULL } object(Foo)#2 (2) { ["a":"Foo":private]=> int(0) ["b":"Foo":private]=> int(1) } Fatal error: Uncaught Error: Cannot access private property Foo::$a in /in/FjDPk:39 Stack trace: #0 {main} thrown in /in/FjDPk on line 39
Process exited with code 255.
Output for 5.4.12 - 5.6.28
object(Foo)#2 (2) { ["a":"Foo":private]=> NULL ["b":"Foo":private]=> NULL } object(Foo)#2 (2) { ["a":"Foo":private]=> int(0) ["b":"Foo":private]=> int(1) } Fatal error: Cannot access private property Foo::$a in /in/FjDPk on line 39
Process exited with code 255.
Output for 5.3.22 - 5.3.29
Fatal error: Call to undefined method ReflectionClass::newInstanceWithoutConstructor() in /in/FjDPk on line 21
Process exited with code 255.