[英]Automatically invoke __call of parent class when invoking public child method?
I have the following code: 我有以下代码:
class A
{
public function __call($method, $args)
{
echo 'Hello';
}
}
class B extends A
{
public function test()
{
echo 'Hello world';
}
}
$b = new B();
b->test(); //outputs 'Hello world';
Now, when invoke test() the output is: Hello world. 现在,当调用test()时,输出为:Hello world。 But i want it to first echo: Hello world and than i want to echo: Hello of the parent without declaring the test method in class A.
但是我希望它首先回应:Hello world和我想要回应:父类的Hello,而没有声明A类中的测试方法。
How can i resolve this? 我该如何解决这个问题?
Edited: 编辑:
Sorry maybe i wasn't clear enough and sorry for my bad english i'm from Holland, what i want is when a child's method gets invoked that the parent class gets the name of the child's method that is invoked. 对不起,也许我不够清楚,抱歉我的英语不好我来自荷兰,我想要的是当一个孩子的方法被调用,父类得到被调用的孩子的方法的名称。
__call is an overload function. __call是一个重载函数。 Meaning that is going to get called when there is no public function defined!!!
当没有定义公共函数时将要调用的含义!
So to get hello only call any other function that is not defined 所以要打招呼只调用任何其他未定义的函数
$b = new B();
b->notAFunction();
Will give you hello only. 只会给你打招呼。
You can just call parent::test()
this way : 你可以这样调用
parent::test()
:
class B extends A {
public function test()
{
echo 'Hello world';
parent::test();
}
}
Outputs : 产出:
Hello WorldHello
You could rename __call to test and call parent::test() in the beginning of the child test. 您可以重命名__call以测试并在子测试开始时调用parent :: test()。
There is no way to automate this. 没有办法自动化这个。
Also like to point out that if you name __call with the two underscores, the access modifier should be private, and _call for protected. 还要指出,如果使用两个下划线命名__call,则访问修饰符应为私有,_call为protected。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.