php – 当被覆盖时如何调用父方法?

前端之家收集整理的这篇文章主要介绍了php – 当被覆盖时如何调用父方法?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
问题可能很简单,但我没有找到解决方案.

这是我的代码

class foo {
 $text = "wordl\n";
 protected function test() {
  echo $this->text;
 }
}

class foo2 extends foo {
 public function test() {
  echo "Hello,";
  parent::test(); //Hmm ... I cant use parent::test() bcs in my case foo::test() require object data from $this
 }
}

$x = new foo2;
$x->test();

foo :: test()方法被foo2 :: test()覆盖.是否可以调用foo :: test()形式foo2 :: test()?

使用parent :: before方法名称,例如
parent::test();

parent.

原文链接:https://www.f2er.com/php/139709.html

猜你在找的PHP相关文章