So I got this idea from Laravel. With Laravel you can do something like the following.
$user = new User;
$user->where('name', 'george')->get();
// which is the same as...
User::where('name', 'george')->get();
So I'm guessing the User class has a __callStatic setup so it makes a new instance as a fallback. I was able to replicate that using the following code.
class Driver
{
protected static $instance = null;
public static function __callStatic($name, $args) {
$classname = get_called_class();
if (empty(static::$instance)) static::$instance = new $classname;
return static::$instance->$name(...$args);
}
}
But a problem occurs when I try to inherit the class more than once. I want all classes to be able to inherit the __callStatic and be able to call any of their ancestor's public functions statically.
class A extends Driver
{
public function hello() {
echo "hello";
return $this;
}
public function world() {
echo " world";
return $this;
}
}
class B extends A
{
public function name() {
echo "\nMy Name is George";
return $this;
}
}
class C extends B
{
// class can be empty
}
C::hello()->world()->name();
// returns: hello world
// My name is George