3v4l.org

run code in 300+ PHP versions simultaneously
<?php class A { private $ref; function __construct () { $this->ref = new class() implements object { public $name; public function sayName() : string { return $this->name; } }; } public function create (string $name) : object { $this->ref->name = $name; return clone $this->ref; } } $A = new A(); $testA = $A->create("matti"); $testB = $A->create("Pekka"); var_dump($testA->sayName()); var_dump($testB->sayName());

preferences:
69.43 ms | 402 KiB | 5 Q