jishan
jishan

Reputation: 300

php - constructor of parent class is not being invoked from child class

parent class:

<?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/>";
        }
    }
}

child class:

<?php

class ExtendedEmailer extends Emailer {

    function __construct() {}

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

Now I am doing this

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

but this gives me the following errors...

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

but when I move the line $this->recipients = array(); inside the scope of addRecipient method it works. That means the constructor of the parent class is not invoked. The concept is to initialize the $recipient variable with an empty array when any object is created. Is it the normal behaviour or not.

modified parent class:

<?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/>";
        }
    }
}

This gives the following output:

Mail successfully sent to [email protected]

And what I am actually trying to do is learning how to access a protected property of a parent class from a child class. Which is $sender in this case.

Upvotes: 1

Views: 84

Answers (1)

Rex Rex
Rex Rex

Reputation: 1030

You have to use parent::__construct(); in child constructor

class ExtendedEmailer extends Emailer {

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

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

Upvotes: 3

Related Questions