我的情况最好用一些代码来描述:
class Foo {
function bar () {
echo "called Foo::bar()";
}
}
class SubFoo extends Foo {
function __call($func) {
if ($func == "bar") {
echo "intercepted bar()!";
}
}
}
$subFoo = new SubFoo();
// what actually happens:
$subFoo->bar(); // "called Foo:bar()"
// what would be nice:
$subFoo->bar(); // "intercepted bar()!"
我知道我可以通过重新定义来让它发挥作用bar()
(以及所有其他相关方法)在子类中,但就我的目的而言,如果__call
函数可以处理它们。它只会让事情变得lot更整洁、更易于管理。
这在 PHP 中可能吗?
__call() https://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods仅当未找到该函数时才调用,因此您的示例(如所写)是不可能的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)