加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 14:03:47 所属栏目:PHP教程 来源:网络整理
导读:问题可能很简单,但我没有找到解决方案. 这是我的代码: class foo { $text = "wordln"; protected function test() { echo $this-text; }}class foo2 extends foo { public function test() { echo "Hello,"; parent::test(); //Hmm ... I cant use parent:
问题可能很简单,但我没有找到解决方案.

这是我的代码:

class foo {
 $text = "wordln";
 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读