Using array_push in method in php

前端 未结 5 1724
抹茶落季
抹茶落季 2021-01-22 05:38

The addRecipients use push_array but it does not work. What am I doing wrong here??

In class.emailer.php

class Emailer
{
public $sender;
public $recipien         


        
相关标签:
5条回答
  • 2021-01-22 06:18

    You need to call the parent's constructor from ExtendedEmailer's constructor.

    class ExtendedEmailer extends emailer
    {
      function  __construct(){
        parent::__construct(null); 
      }
    
      // ...
    }
    

    otherwise $recipients is never initialized as an array.

    0 讨论(0)
  • 2021-01-22 06:32

    You've missed to the call the parent constructor in the child's constructor

    class ExtendedEmailer extends emailer
    {
        function  __construct(){
            //overwriting __contruct here
            parent::__construct('dummy_sender@sender.com');
        }
    }
    

    should fix this issue.

    0 讨论(0)
  • 2021-01-22 06:32

    in file "class.emailer.php" just do

    public $recipients = array();
    

    instead of just the kinda naked

    public $recipients;
    

    That's all, and as a sur-plus, you're free to fill up your constructors as you desire. - What do you think about that?

    Regards, M.

    0 讨论(0)
  • 2021-01-22 06:39

    The problem is that you're overriding the Email class's constructor and never calling it (parent::__construct()). Either call the parent constructor in the ExtendedEmailer class constructor, or remove the constructor altogether from ExtendedEmailer if you're not actually using it (which in your sample code, you're not).

    0 讨论(0)
  • 2021-01-22 06:43

    You are probably not initiating $recipients when you rewrote the constructor.

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

    Should do it. Check your warnings for

    Warning: array_push() expects parameter 1 to be array, null given in ...

    0 讨论(0)
提交回复
热议问题