- Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
- Foo::bar<br />Foo::baz<br />Foo::bar<br />Foo::baz<br />
<?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 $this->$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();