3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class Hoge { private $_name; private $_var; private $_default = "class default"; private function __construct($name) { $this->_name = $name; $this->_var = time(); echo "Side effect"; } } function callPrivateConstructor($class, $args = array()) { $dried = serialize(new stdClass); $fresh = unserialize(str_replace( '8:"stdClass"', strlen($class) . ':"' . $class . '"', $dried )); assert(is_a($fresh, $class)); $ref = new ReflectionClass($class); $constructor = $ref->getConstructor(); $constructor->setAccessible(true); $constructor->invokeArgs($fresh, $args); return $fresh; } $a = callPrivateConstructor("Hoge", array("aaaa")); var_dump($a);
Output for 5.3.2 - 7.1.0
Side effectobject(Hoge)#1 (3) { ["_name":"Hoge":private]=> string(4) "aaaa" ["_var":"Hoge":private]=> int(1387617740) ["_default":"Hoge":private]=> string(13) "class default" }
Output for 5.3.0 - 5.3.1
Fatal error: Call to undefined method ReflectionMethod::setAccessible() in /in/86aR4 on line 26
Process exited with code 255.
Output for 5.0.0 - 5.2.17
Strict Standards: is_a(): Deprecated. Please use the instanceof operator in /in/86aR4 on line 22 Fatal error: Call to undefined method ReflectionMethod::setAccessible() in /in/86aR4 on line 26
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/86aR4 on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/86aR4 on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/86aR4 on line 3
Process exited with code 255.