Several ways to call non-static method from static method:
One way is to use
new
keyword followed by the class name
(new Foo)->nonStaticMethod();
Another method is to use
self
which comes after
new
keyword
(new self)->nonStaticMethod();
Complete example:
class Foo {
public function nonStaticMethod()
{
return 'non-static';
}
public static function staticMethod()
{
// return (new Foo)->nonStaticMethod();
return (new self)->nonStaticMethod();
}
}
echo Foo::staticMethod();
The second method
new self
is better, because if we want to rename the class later, we will not need to change the class name more than one place