Is $ this & # 8594; __ invoke (); 100% safe and valid in PHP?
1 answer
This shouldn't work as there is no method in your class __invoke()
:
class SomeClass {
public function test()
{
$this->__invoke();
}
}
$inst = new SomeClass();
$inst->test();
If you are implementing __invoke()
this should work:
class SomeClass {
public function __invoke()
{
var_dump('Invoke!');
}
}
$inst = new SomeClass();
$inst();
Magic methods can be called directly, as you can see in the second test, but this is not a good idea in my opinion as they are kind of hooks and their code can be executed unexpectedly.
+3
source to share