3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php //implementácia rozhrania Mediator //metoda odoslat() je volaná zamestnancom a slúži k odoslaniu správ interface Mediator { public function send($message, AbstractEmployee $employee); } //definovanie rozhrania abstraktnej triedy AbstractEmployee //trieda obsahuje jedinečný atribút - identifikátor ID, metódu pre čítanie a zápis atribútu //metóda send($message) volá metódu send() sprostredkovateľa na odosielanie správ //metóda receive($message) prijíma správy a je definovaná pre každého potomka triedy //AbstractEmployee abstract class AbstractEmployee { protected $ID; protected $mediator; public function __construct ($ID, Mediator $mediator) { $this -> SetID($ID); $this -> SetMediator($mediator); } public function SetID($ID) { $this -> ID = $ID; } public function GetID() { return $this -> ID; } public function SetMediator(Mediator $mediator) { $this -> mediator = $mediator; } public function GetMediator() { return $this -> mediator ; } public function send($message) { $this -> mediator -> send($message, $this); } public abstract function receive($message); } //trieda Mediator – zabezpečuje registráciu zamestnancov, odosielanie a prijímanie správ class NewMediator implements Mediator { protected $employee = array(); public function registerEmployee (AbstractEmployee $employee) { $this -> employees[] = $employee; } public function send($message, AbstractEmployee $sender) { foreach ($this -> employees as $employee) { if($employee !== $sender) { $employee ->receive($message); } } } } //potomok triedy AbstractEmployee – trieda Employee //metódou receive($message) sa volá sprostredkovateľ, ktorému sa odovzdá posielaná správa class Employee extends AbstractEmployee { public function receive($message) { printf("Prijatie správy: \n", $this -> GetID, $message); } } //testovací script $mediator = new NewMediator(); $employee1 = new Employee('Zamestnanec 1', $mediator); $employee2 = new Employee('Zamestnanec 2', $mediator); $employee3 = new Employee('Zamestnanec 3', $mediator); $employee3 = new Employee('Zamestnanec 4', $mediator); $mediator -> registerEmployee($employee1); $mediator -> registerEmployee($employee2); $mediator -> registerEmployee($employee3); $mediator -> registerEmployee($employee4); $employee3 -> send('Správa zaslaná od ZAM3'); $employee1 -> send('Správa prijatá od ZAM1'); ?>
based on U6NSQ
Output for 7.0.0 - 7.2.0
Notice: Undefined variable: employee4 in /in/RCrOb on line 97 Fatal error: Uncaught TypeError: Argument 1 passed to NewMediator::registerEmployee() must be an instance of AbstractEmployee, null given, called in /in/RCrOb on line 97 and defined in /in/RCrOb:60 Stack trace: #0 /in/RCrOb(97): NewMediator->registerEmployee(NULL) #1 {main} thrown in /in/RCrOb on line 60
Process exited with code 255.
Output for 5.3.19 - 5.6.21
Notice: Undefined variable: employee4 in /in/RCrOb on line 97 Catchable fatal error: Argument 1 passed to NewMediator::registerEmployee() must be an instance of AbstractEmployee, null given, called in /in/RCrOb on line 97 and defined in /in/RCrOb on line 60
Process exited with code 255.