父类方法在其子类__construct()中返回null


parent class method returns null in its daughter __construct()

我尝试了一个代码,我在它的子__construct中调用父方法,它返回NULL,我不知道为什么?如果有人能告诉我原因,我会很高兴。提前谢谢。

这是我的代码

 <?php
 class me
 {
   public $arm;
   public $leg;
   public function __construct()
   {
     $this->arm = 'beautiful';
     $this->leg = 'pretty';
   }
   public function setLeg($l)
   {
     $this->leg = $l;
   }
   public function getLeg()
   {
     return $this->leg;
   }
 }
 class myBio extends me
{
  public $bio;
  public function __construc()
  {
    $this->bio = $this->setLeg();
  }
  public function newLeg()
  {
    var_dump($this->bio);
  }
  public function tryLeg()
  {
    $this->leg = $this->getLeg();
    print $this->leg;
  }
}
$mB = new myBio();
$mB->newLeg();
$mB->tryLeg();
 ?>

当我调用时:$mB = new myBio();mB -> newLeg ();

,它返回空,

$mB->tryLeg();

返回字符串'pretty'.

这一行有个打字错误:

$this->bio = $this->setLeg();

你调用的是setter,而不是getter,因为setter没有返回值,所以你得到的是null。

你还拼错了construct:
     public function __construc()

你需要调用父构造函数。

<?php
class me
{
     public $arm;
     public $leg;
     public function __construct()
     {
          $this->arm = 'beautiful';
          $this->leg = 'pretty';
     }
     public function setLeg($l)
     {
          $this->leg = $l;
     }
     public function getLeg()
     {
          return $this->leg;
     }
}
class myBio extends me
{
    public $bio;
    public function __construct()
    {
         parent::__construct();
         $this->bio = $this->getLeg();
    }
    public function newLeg()
    {
         var_dump($this->bio);
    }
    public function tryLeg()
    {
         $this->leg = $this->getLeg();
         print $this->leg;
    }
}
$mB = new myBio();
$mB->newLeg();
$mB->tryLeg();