php - 调用重写的父方法

标签 php object overriding

在下面的示例代码中,父类 Foo 中的方法 test() 被子类中的方法 test() 覆盖。是否可以从 Bar::test() 调用 Foo::test()

class Foo 
{
  $text = "world\n";

  protected function test() {
    echo $this->text;
  }
}// class Foo

class Bar extends Foo 
{
  public function test() {
    echo "Hello, ";

    // Cannot use 'parent::test()' because, in this case,
    // Foo::test() requires object data from $this
    parent::test();
  }
}// class Bar extends Foo

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

最佳答案

在方法名前使用parent::,例如

parent::test();

参见 parent

关于php - 调用重写的父方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8073471/

相关文章:

PHP :exif_read_data() not defined

php - Websocket:如何在 C 中编码文本以发送给客户端

javascript - 如何检查 JavaScript 中对象中是否存在值

Python 覆盖字典访问方法

javascript - 覆盖 JavaScript 中比较运算符的默认行为

c# - 为什么重写的 ToString() 在项目添加到 ComboBox 时不返回我想要的内容?

php - mysql_query 不返回密码

php - 如何使用 PHP 在行范围内显示 mysql 数据?

java - 在java中按值传递对象

javascript - 映射复杂的 JavaScript 对象数组以获取值