Php 如何呼叫家长';s方法,该方法在子对象中重载

Php 如何呼叫家长';s方法,该方法在子对象中重载,php,class,parent,superclass,super,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 pa

我有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;
  }

}

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

旁注:您可以移动
public$id
AParent
类的声明。
$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"