3v4l.org

run code in 300+ PHP versions simultaneously
<?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 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.25, 7.3.0 - 7.3.12, 7.4.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.0.5, 5.1.0 - 5.1.6, 5.2.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.3.11, 4.4.0 - 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.

preferences:
215.32 ms | 401 KiB | 248 Q