在PHP5虚拟功能共享
请看下面的代码:复制代码代码如下所示:
< PHP
类{
公共函数x(){
被调用。;
}
公共函数y(){
自我::x();
调用a::y()。;
}
公共函数Z(){
$ $ x(x);
调用a:Z()。;
}
}
B类扩展{
公共函数x(){
被调用。;
}
}
新的b();
$ B = y();
;
$ - Z();
>
在这种情况下,a::(y)调用a::(x),并且:(x::x())用一个:(x)表示,然后调用B::y(),B::(y)应该调用或失败()In C++, if A:: X () is not defined as a virtual function, then B:: Y () (that is A:: Y ()) will invoke A:: X (), and if A:: X (), use x keyword to be defined as a virtual function, then: : () will call :.In PHP5, however, the function of the virtual function is implemented by the self and $this keywords.If the parent class: Y (A:): X (self:) used in the way of calling A:: X (), so in the sub class of A: (x): no matter whether covered, A:: Y () call is A:: X (); and if the parent class A:: Y ($this->x) () calls the A:: X (), so if the subclass of A:: X (B:) is: X () A: (y): coverage, will call the B: (x).
以上结果如下:
答:X()被称为。答:被称为y()。——
B::X()被称为。答:被称为Z()。
virtual-function.php
复制代码代码如下所示:
< PHP
类parentclass {
静态公共函数说($ STR){
静态::do_print($str);
}
静态公共功能do_print($str){
父;
}
}
子类扩展parentclass {
静态公共功能do_print($str){
孩子说:;
}
}
类anotherchildclass延伸parentclass {
静态公共功能do_print($str){
回声anotherchild说$str ;
}
}
phpversion(回声);
美元=新ChildClass();
$(你好)->说;
$ =新AnotherChildClass();
$ B ->说(你好);