- method_exists: documentation ( source)
<?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();