<?php
class Foo
{
private $bar;
private $baz;
protected function getBar()
{
return __METHOD__;
}
private function getBaz()
{
return __METHOD__;
}
public function __call($methodName, $args)
{
if (!method_exists($this, $methodName)) {
return 'some wise behavior';
}
return 'Magic! '.$this->$methodName(...$args);
}
}
$foo = new Foo();
var_dump(method_exists($foo, '__call'));
var_dump(method_exists($foo, 'bar'));
var_dump(method_exists($foo, 'baz'));
var_dump($foo->__call('baz', []));
var_dump($foo->baz());
var_dump($foo->__call('nonexistent', []));
- Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
- bool(true)
bool(false)
bool(false)
string(18) "some wise behavior"
string(18) "some wise behavior"
string(18) "some wise behavior"
preferences:
112.14 ms | 408 KiB | 5 Q