Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Dziedziczenie i pętla foreach
Forum PHP.pl > Forum > PHP > Object-oriented programming
MicNeo
Witam!

Raczkuje w OOP. Natknąlem się na taki błąd:
Cytat
Warning: Invalid argument supplied for foreach() in /homez.108/micneo/www/test/class.emailer.php on line 43


class.emailer.php
  1. <?
  2. // class.emailer.php
  3. class Emailer
  4. {
  5.   private $sender;
  6.   private $recipients;
  7.   private $subject;
  8.   private $body;
  9.   function __construct($sender)
  10.   {
  11.      $this->sender = $sender;
  12.      $this->recipients = array();
  13.   }
  14.  
  15.   public function addRecipients($recipient)
  16.   {
  17.      array_push($this->recipients, $recipient);
  18.   }
  19.   public function setSubject($subject)
  20.   {
  21.      $this->subject = $subject;
  22.   }
  23.   public function setBody($body)
  24.   {
  25.      $this->body = $body;
  26.   }
  27.   public function sendEmail()
  28.   {
  29.      foreach ($this->recipients as $recipient)
  30.      {
  31.         $result = mail($recipient, $this->subject, $this->body,
  32.            "From: {$this->sender}r\n");
  33.         if ($result) echo "Wiadomość została wysła do
  34.            {$recipient}<br/>";
  35.      }
  36.   }
  37. }
  38.  
  39. class HtmlEmailer extends Emailer
  40. {
  41.   public function sendHTMLEmail()
  42.   {
  43.         foreach ($this->recipients as $recipient)
  44.         {
  45.            $headers  = 'MIME-Version: 1.0' . "r\n";
  46.            $headers .= 'Content-type: text/html; charset=iso-8859-2' .
  47.               "r\n";
  48.            $headers .= 'From: {$this->sender}' . "r\n";
  49.            $result = mail($recipient, $this->subject, $this->body,
  50.               $headers);
  51.            if ($result) echo "Wiadomość w formacie HTML została wysłana do
  52.               {$recipient}<br/>";
  53.      }
  54.   }
  55. }
  56. ?>


oraz plik emailer.php
  1. <?php
  2. include_once("class.emailer.php");
  3.  
  4. $mailHTML = new HtmlEmailer("adres@mail.pl");
  5. $mailHTML->addRecipients("odbioraca@mail.pl");
  6. $mailHTML->setSubject("temat");
  7. $mailHTML->setBody("test");
  8. $mailHTML->sendHTMLEmail();
  9.  
  10. ?>


Nie wiem co jest nie tak. Jak się wczesniej bawiłem dziedziczeniem to śmigało (nie wykorzystalem pętli foreach). Nie wiem co źle robie. Prosze o pomoc.

Pozdrawiam!
pyro
  1. <?php
  2. private $recipients;
  3. ?>


Zamień na:

  1. <?php
  2. protected $recipients;
  3. ?>


// edit

i podobnie reszte pól klasy!

  1. <?php
  2. private $sender;
  3.  private $recipients;
  4.  private $subject;
  5.  private $body;
  6. ?>


na:

  1. <?php
  2. protected $sender;
  3.  protected $recipients;
  4.  protected $subject;
  5.  protected $body;
  6. ?>


// add

mały hint

  1. <?php
  2. function __construct($sender)
  3.   {
  4.      $this->sender = $sender;
  5.      $this->recipients = array();
  6.   }
  7. ?>


Nie musisz robić (tutaj) drugiej instrukcji w konstruktorze, możesz od razu dać

  1. <?php
  2. protected $recipients = array();
  3. ?>
MicNeo
facepalm...

Cholera, nie zauważyłem tego winksmiley.jpg Dzięki.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.