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 );
Output for 5.2.3 - 5.2.17, 5.3.0 - 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.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.15, 8.5.0 - 8.5.1
1 - construct 1 - bar - stream resource(5) of type (stream) 2 - construct 2 - bar - stream 2 - destruct resource(6) of type (Unknown) 1 - destruct
Output for 5.1.4 - 5.1.6, 5.2.0 - 5.2.2
1 - construct 1 - bar - stream resource(2) of type (stream) 2 - construct 2 - bar - stream 2 - destruct resource(3) of type (Unknown) 1 - destruct
Output for 5.0.2 - 5.0.5, 5.1.0 - 5.1.3
Warning: fopen(php://memory): failed to open stream: Success in /in/NjpYP on line 9 1 - construct Warning: Supplied argument is not a valid resource handle in /in/NjpYP on line 21 1 - bar - bool(false) Warning: fopen(php://memory): failed to open stream: Success in /in/NjpYP on line 9 2 - construct Warning: Supplied argument is not a valid resource handle in /in/NjpYP on line 21 2 - bar - 2 - destruct Warning: fclose(): supplied argument is not a valid stream resource in /in/NjpYP on line 17 bool(false) 1 - destruct Warning: fclose(): supplied argument is not a valid stream resource in /in/NjpYP on line 17
Output for 5.0.0 - 5.0.1
Warning: fopen(php://memory): failed to open stream: Success in /in/NjpYP on line 9 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NjpYP on line 11 1 - constructPHP_EOL Warning: Supplied argument is not a valid resource handle in /in/NjpYP on line 21 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NjpYP on line 21 1 - bar - PHP_EOLbool(false) Warning: fopen(php://memory): failed to open stream: Success in /in/NjpYP on line 9 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NjpYP on line 11 2 - constructPHP_EOL Warning: Supplied argument is not a valid resource handle in /in/NjpYP on line 21 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NjpYP on line 21 2 - bar - PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NjpYP on line 15 2 - destructPHP_EOL Warning: fclose(): supplied argument is not a valid stream resource in /in/NjpYP on line 17 bool(false) Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NjpYP on line 15 1 - destructPHP_EOL Warning: fclose(): supplied argument is not a valid stream resource in /in/NjpYP on line 17
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/NjpYP on line 4
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/NjpYP on line 4
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/NjpYP on line 4
Process exited with code 255.

preferences:
183.28 ms | 416 KiB | 5 Q