Hej, przerabiam książkę: "Programownie obiektowe w PHP5" - Hasin Hayder. Niestety mam problem. Nie bardzo wiem z czego on wynika, bo jestem na początku drogi (2 rozdział, modyfikator protected) a kod przepisałem po prostu(stosując tylko swojekonwencje zapisu zmiennych, klamer etc). Otóż otrzymuję dwa ostrzeżenia i nie wiem jak się ich pozbyć:
Warning: array_push() expects parameter 1 to be array, null given in /opt/lampp/htdocs/oop/2/class.emailer.php on line 11
Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/oop/2/class.emailer.php on line 20
Dodatkowo użyłem funkcji var_dump()
aby zwrócić zawartość obiektu i oto co otrzymuję:
object(ExtendEmailer)#1 (5) { ["sender":protected]=> string(22) "[email protected]" ["recipient":"Emailer":private]=> NULL ["subject":"Emailer":private]=> string(15) "To tylko tekst " ["body":"Emailer":private]=> string(32) "Cześć Hasin, Jak się miewasz?" ["recipients"]=> NULL }
Tak wygląda ko:
Plik: class.emailer.php
<?php
class Emailer {
protected $sender;
private $recipient, $subject, $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.'');
if ($result) {
echo 'Wiadomość została wysłana do '.$recipient.'<br/>';
}
}
}
}
?>
Plik: class.ExtendEmailer.php
<?php
class ExtendEmailer extends Emailer {
function __construct() {
}
public function setSender($sender) {
$this->sender = $sender;
}
}
?>
Plik: index.php
<?php
include_once('class.emailer.php');
include_once('class.extendEmailer.php');
$xemailer = new ExtendEmailer;
$xemailer->setSender("[email protected]");
$xemailer->addRecipients("[email protected]");
$xemailer->setSubject('To tylko tekst ');
$xemailer->setBody('Cześć Hasin, Jak się miewasz?');
$xemailer->sendEmail();
var_dump($xemailer);
?>
Odpalam plik index.php i to się dzieje. Co robię nie tak? A może od PHP5 do PHP7 się pozmieniało na tyle, że książka jest nieaktualna?