3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { private $foo; private function __construct($spam){ $this->foo = $spam; } public function getFoo() { return $this->foo; } } function instantiatePrivateCtor($class /*, $...args */) { $args = array_slice(func_get_args(), 1); $extClass = $class . '__Ext'; if (!class_exists($extClass)) { $ns = ltrim(dirname($extClass), '\\'); $name = basename($extClass); $namespace = $ns && $ns !== '.' ? 'namespace ' . $ns . ';' : ''; eval(" $namespace class $name extends $class { public function __construct() { \$args = func_get_args(); call_user_func_array('parent::__construct', \$args); } } "); } $reflClass = new \ReflectionClass($extClass); return $reflClass->newInstanceArgs($args); } $args = 'bacon'; $class = 'Foo'; $reflClass = new \ReflectionClass($class); $instance = instantiatePrivateCtor($class, 'bacon'); echo $instance->getFoo();
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Use of "parent" in callables is deprecated in /in/ff6q0(26) : eval()'d code on line 6 Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, cannot access private method Foo::__construct() in /in/ff6q0(26) : eval()'d code:6 Stack trace: #0 [internal function]: Foo__Ext->__construct('bacon') #1 /in/ff6q0(36): ReflectionClass->newInstanceArgs(Array) #2 /in/ff6q0(43): instantiatePrivateCtor('Foo', 'bacon') #3 {main} thrown in /in/ff6q0(26) : eval()'d code on line 6
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
45.02 ms | 402 KiB | 8 Q