php - 没有从子类调用父类的构造函数

时间:2015-09-22 10:15:42

标签: php class oop

父类:

<?php

class Emailer {

    protected $sender;
    private $recipients;
    private $subject;
    private $body;

    function __construct($sender)
    {
        $this->sender = $sender;
        $this->recipients = array();
    }

    public function addRecipients($recipient)
    {
        array_push($this->recipients, $recipient);
    }

    public function setSubject($subject)
    {
        $this->subject = $subject;
    }

    public function setBody($body)
    {
        $this->body = $body;
    }

    public function sendEmail()
    {
        foreach($this->recipients as $recipient)
        {
            $result = mail($recipient, $this->subject, $this->body, "From: {$this->sender}\r\n");
            if($result)
                echo "Mail successfully sent to {$recipient}" . "<br/>";
        }
    }
}

儿童班:

<?php

class ExtendedEmailer extends Emailer {

    function __construct() {}

    public function setSender($sender)
    {
        $this->sender = $sender;
    }
}

现在我正在做这个

include_once("classes/class.emailer.php");
include_once("classes/class.extendedemailer.php");
$xemailer = new ExtendedEmailer();
$xemailer->setSender("someone@example.com");
$xemailer->addRecipients("person1@example.com");
$xemailer->setSubject("Just a Test");
$xemailer->setBody("Hi person1, How are you?");
$xemailer->sendEmail();

但这给了我以下错误......

Warning: array_push() expects parameter 1 to be array, null given in C:\xampp\htdocs\oop\classes\class.emailer.php on line 19

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\oop\classes\class.emailer.php on line 34

但是当我在$this->recipients = array();方法的范围内移动addRecipient行时,它可以工作。这意味着不会调用父类的构造函数。这个概念是在创建任何对象时用空数组初始化$recipient变量。这是否是正常行为。

修改后的父类:

<?php

class Emailer {

    protected $sender;
    private $recipients;
    private $subject;
    private $body;

    function __construct($sender)
    {
        $this->sender = $sender;
    }

    public function addRecipients($recipient)
    {
        $this->recipients = array();
        array_push($this->recipients, $recipient);
    }

    public function setSubject($subject)
    {
        $this->subject = $subject;
    }

    public function setBody($body)
    {
        $this->body = $body;
    }

    public function sendEmail()
    {
        foreach($this->recipients as $recipient)
        {
            $result = mail($recipient, $this->subject, $this->body, "From: {$this->sender}\r\n");
            if($result)
                echo "Mail successfully sent to {$recipient}" . "<br/>";
        }
    }
}

这给出了以下输出:

Mail successfully sent to person1@example.com

我实际上要做的是学习如何从子类访问父类的受保护属性。在这种情况下,这是$sender

1 个答案:

答案 0 :(得分:3)

您必须在子构造函数

中使用parent::__construct();
class ExtendedEmailer extends Emailer {

       function __construct($sender)
        {
           parent::__construct($sender);
        }

        public function setSender($sender)
        {
            $this->sender = $sender;
        }
    }