如何调用在子级中重载的父方法

时间:2013-06-20 10:11:04

标签: php class parent superclass super

我有PHP代码

<?
class AParent {

  function to_s() {
    return $this->id;
  }

}

class AChild {

  public $id;
  public $name;

  function to_s() {
    if(!empty($this->name)) {
      $ret = $this->name;
    } else {
      $ret = /** call parent's method to_s() */
    }
    return $ret;
  }

}

如何调用在孩子中超载的父母方法? :)有可能使用静态方法,但如何使用非静态方法?

$a = new AChild();
$a->id = 1;
$a->name = 'Something';
echo $a; // Should echo "Something"

$b = new AChild();
$b->id = 2;
echo $b; // Should echo "2"

1 个答案:

答案 0 :(得分:0)

使用$ret = parent::to_s(),就够了

相关问题