是否可以设置一个类,以便在未定义方法时不会抛出错误,而是转到一个包罗万象的函数?
如果我调用 $myClass->foobar();
但 foobar 从未在类定义中设置,其他一些方法会处理它吗?
最佳答案
是的,是overloading :
class Foo {
public function __call($method, $args) {
echo "$method is not defined";
}
}
$a = new Foo;
$a->foo();
$b->bar();
从 PHP 5.3 开始,您还可以使用静态方法来完成此操作:
class Foo {
static public function __callStatic($method, $args) {
echo "$method is not defined";
}
}
Foo::hello();
Foo::world();
关于PHP - 类中的 catchall 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6254779/