
<?php // class.emailer.php class Emailer { protected $sender; private $recipients; private $subject; private $body; function __construct($sender) { $this->sender = $sender; } public function addRecipients($recipient) { } public function setSubject($subject) { $this->subject = $subject; } public function setBody($body) { $this->body = $body; } public function sendEmail() { foreach ($this->recipients as $recipient) { "From: {$this->sender}\r\n"); {$recipient}<br/>"; } } } class ExtendedEmailer extends Emailer { function __construct(){} public function setSender($sender) { $this->sender = $sender; } } $xemailer = new ExtendedEmailer(); $xemailer->setSender("JA"); $xemailer->addRecipients("user1@localhost"); $xemailer->addRecipients("user2@localhost"); $xemailer->setSubject("extended email"); $xemailer->setBody("<h1>Extended</h1>"); $xemailer->sendEmail(); ?>
Wynik skryptu wygląda tak:
Warning: array_push() expects parameter 1 to be array, null given in C:\xampp\htdocs\OOP\ex2.1.php on line 18 Warning: array_push() expects parameter 1 to be array, null given in C:\xampp\htdocs\OOP\ex2.1.php on line 18 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\OOP\ex2.1.php on line 30