Php 父类方法在其子_构造()中返回null

Php 父类方法在其子_构造()中返回null,php,oop,Php,Oop,我尝试了一个代码,我在它的子结构中调用了一个父方法,并且itreturns为NULL, 我不知道为什么?如果有人能向我解释原因,我会非常高兴。 提前谢谢 这是我的代码 返回e字符串“pretty”。您在这一行有一个输入错误: $this->bio = $this->setLeg(); 您调用的是setter,而不是getter,因为setter不返回值,所以您得到的是null 您还拼错了构造: public function __construc() 您需要调用父构造函

我尝试了一个代码,我在它的子结构中调用了一个父方法,并且itreturns为NULL, 我不知道为什么?如果有人能向我解释原因,我会非常高兴。 提前谢谢

这是我的代码


返回e字符串“pretty”。

您在这一行有一个输入错误:

$this->bio = $this->setLeg();
您调用的是setter,而不是getter,因为setter不返回值,所以您得到的是null

您还拼错了构造:

     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();

$this->bio=$this->setLeg()输入错误,
设置!=get
@scragar:应该是答案,因为它应该适用于打字错误fixed@niconoe我把它作为一个评论发布,因为我甚至没有读过这个问题,我只是在滚动浏览,注意到一个setter在没有参数的情况下使用。据我当时所知,这个问题与我的评论完全无关,而且评论是偶然的。非常感谢。但我纠正了那个打字错误,但它还是回来了NULL@Dr.Professor,检查答案。
     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();