3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { private $id; public $handle; public function __construct($id) { $this->id = $id; $this->handle = fopen('php://memory', 'r+'); echo $this->id . ' - construct' . PHP_EOL; } public function __destruct() { echo $this->id . ' - destruct' . PHP_EOL; fclose($this->handle); } public function bar() { echo $this->id . ' - bar - ' . get_resource_type($this->handle) . PHP_EOL; return $this; } public static function create($id) { return new Foo($id); } } $foo = Foo::create(1); var_dump( $foo->bar()->handle ); var_dump( Foo::create(2)->bar()->handle );

preferences:
37.37 ms | 407 KiB | 5 Q