本文介绍了PHP的魔术方法__call子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的情况最好用一些代码来描述:
My situation is best described with a bit of code:
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
函数可以处理它们,那就太好了.它只是使事情变得更整洁,更易于管理.
I know I can get this to work by redefining bar()
(and all the other relevant methods) in the sub-class, but for my purposes, it'd be nice if the __call
function could handle them. It'd just make things a lot neater and more manageable.
这在PHP中可行吗?
推荐答案
__call()
仅在找不到该函数的情况下才被调用,因此您编写的示例无法实现.
__call()
is only invoked when the function isn't otherwise found so your example, as written, is not possible.
这篇关于PHP的魔术方法__call子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!