在 PHP 中调用超级方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3754952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:59:39  来源:igfitidea点击:

Calling a super method in PHP

phpoop

提问by Emanuil Rusev

Can you do something like this in PHP:

你能在 PHP 中做这样的事情吗:

function foo()
{
    super->foo();

    // do something
}

回答by davidtbernal

Yes, it's called parent::though.

是的,parent::虽然它被称为。

public function foo()
{
    parent::foo(); // this is not a static method call, even though it looks like one

    //do something
}

回答by Byron Whitlock

use parent;

使用父级

parent::foo();

parent::foo();

回答by Harold1983-

Do you mean calling the parent class method? In that case you would do:

你的意思是调用父类方法?在这种情况下,你会这样做:

class Bar
{
  public function foo()
  {
    // blah
  }
}


class Baz extends Bar
{
  public function foo() 
  {
    parent::foo();
  }
}