I've found some strange (for me) behavior of the PHP interpreter and I'm not sure if it's safe to use it in production or not.
When we call Foo::bar()
and the Foo
class does not have the static bar
method but it has non-static bar
method, the interpreter will invoke non-static bar
on null
(yes, it sounds ridiculous). I expected the __callStatic
to be invoked in this case. But it's not what is happening for some reason.
I've then found a handy usage for this behavior: to provide the class with static and non-static methods with the same name like this:
class Foo
{
public function bar(){
if (isset($this)) {
$this->nonStaticBar();
} else {
static::staticBar();
}
}
private function nonStaticBar() {
echo "Non-static\n";
}
private static function staticBar() {
echo "Static\n";
}
}
(new Foo())->bar(); // Output: "Non-static"
Foo::bar(); // Output: "Static"
Yes I know, that this approach is not elegant and architecturally wrong. The question is if it's safe (standard-compliant) to use this "feature" or not. Are there any other cases when isset($this)
can equal false
?