3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class DbConnection { private $dsn; private $initializer; public function __construct(string $dsn) { $this->dsn = $dsn; // socket stuff happens here, much like with PDO } public function query(string $queryString) : array { ($this->initializer)(); // irrelevant from here on return ['query' => $queryString, 'dsn' => $this->dsn]; } public static function lazyInstance(string $dsn) : self { $instance = (new ReflectionClass(self::class))->newInstanceWithoutConstructor(); $instance->initializer = function () use ($dsn, $instance) { $instance->__construct($dsn); $instance->initializer = function () { }; }; return $instance; } } $instance = DbConnection::lazyInstance('mysql://something'); var_dump($instance); var_dump($instance->query('SELECT * FROM foo')); var_dump($instance->query('SELECT * FROM bar'));
Output for git.master, git.master_jit, rfc.property-hooks
object(DbConnection)#2 (2) { ["dsn":"DbConnection":private]=> NULL ["initializer":"DbConnection":private]=> object(Closure)#1 (1) { ["static"]=> array(2) { ["dsn"]=> string(17) "mysql://something" ["instance"]=> *RECURSION* } } } array(2) { ["query"]=> string(17) "SELECT * FROM foo" ["dsn"]=> string(17) "mysql://something" } array(2) { ["query"]=> string(17) "SELECT * FROM bar" ["dsn"]=> string(17) "mysql://something" }

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:
18.56 ms | 402 KiB | 8 Q