title: Undefined Property in Class Inheritance?
body: <p>I just started to learn OOP PHP, and i've been done some exercises, and i got a problem with inheritance classes that return these errors:</p>
  
  <pre><code>Notice: Undefined property: HtmlEmailer::$recipients in C:\xampp\htdocs\class.htmlemailer.php on line 10
  
  Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\class.htmlemailer.php on line 10
  </code></pre>
  
  <p>why is Undefined Property? i did define it in emailer.test.php...</p>
  
  <p>i did read 3x the same chapter of my book, inspect each part of code and i didn't realize where the error is.</p>
  
  <p>there is my code: class.emailer.php</p>
  
  <pre><code>&lt;?php
  /**
  * 
  */
  class Emailer
  {
      private $sender;
      private $recipients;
      private $subject;
      private $body;
  
      function __construct($sender)
      {
          $this-&gt;sender = $sender;
          $this-&gt;recipients = array();
      }
  
      public function addRecipients($recipients)
      {
          array_push($this-&gt;recipients, $recipients);
      }
  
      public function setSubject($subject)
      {
          $this-&gt;subject = $subject;
      }
  
      public function setBody($body)
      {
          $this-&gt;body = $body;
      }
  
      public function sendEmail()
      {
          foreach ($this-&gt;recipients as $recipient)
          {
              $result = mail($recipient, $this-&gt;subject, $this-&gt;body, "From: ".$this-&gt;sender."\r\n");
              if ($result)
              {
                  echo "Email successfully sent to ".$recipient;
              }
          }
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>class.htmlemailer.php</p>
  
  <pre><code>&lt;?php
  /**
  * htmlemailer extends emailer
  */
  
  class HtmlEmailer extends Emailer
  {
      public function sendHTMLEmail()
      {
          foreach ($this-&gt;recipients as $recipient)
          {
              $headers = 'MIME-Version: 1.0' . "\r\n";
              $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
              $headers .= 'From: {$this-&gt;sender}' . "\r\n";
  
              $result = mail($recipient, $this-&gt;subject, $this-&gt;body, $headers);
              if ($result)
              {
                  echo "HTML mail successfully sent to ".$recipient." ! &lt;br /&gt;";
              }
          }
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>emailer.test.php</p>
  
  <pre><code>&lt;html&gt;
  
  &lt;?php 
  /**
   * tests
   */
  include_once('class.emailer.php');
  include_once('class.htmlemailer.php');
  
  $emailerobject = new HtmlEmailer('some@emaildotcom');
  $emailerobject-&gt;addRecipients('user@emaildotcom');
  $emailerobject-&gt;setSubject('Some Subject');
  $emailerobject-&gt;setBody('Some body message');
  $emailerobject-&gt;sendHTMLEmail();
  
  ?&gt;
  
  &lt;/html&gt;
  </code></pre>