3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { protected static ?Foo $instance = null; public static function __callStatic($method, $args) { $instance = self::$instance ?? self::$instance = new static(); return $instance->__call($method, $args); } public function __call($method, $args) { if (method_exists($this, $method)) { return $instance->$method(...$args); } return $this; } protected function bar() { echo __METHOD__ . '<br />'; return $this; } protected function baz() { echo __METHOD__ . '<br />'; return $this; } } Foo::bar()->baz(); (new Foo())->bar()->baz();
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Warning: Undefined variable $instance in /in/moH0s on line 16 Fatal error: Uncaught Error: Call to a member function bar() on null in /in/moH0s:16 Stack trace: #0 /in/moH0s(10): Foo->__call('bar', Array) #1 /in/moH0s(37): Foo::__callStatic('bar', Array) #2 {main} thrown in /in/moH0s on line 16
Process exited with code 255.

preferences:
86.13 ms | 402 KiB | 62 Q