3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php interface MediatorInterface { public function send($message, Colleague $from); public function add(Colleague $colleague); } interface SuggestionProviderInterface { public function getSuggestionsFor($data); } class ContactSuggestionProvider implements SuggestionProviderInterface { public function getSuggestionsFor($data) { if (isset($data['customer'])) { return [ 'contact a', // Suggestion 'contact b', // Suggestion 'contact c' // Suggestion ]; } return []; } } class WorkplaceSuggestionProvider implements SuggestionProviderInterface { public function getSuggestionsFor($data) { if (isset($data['customer']) && isset($data['contact'])) { return [ ['workplace a'], // Suggestion ['workplace b'] // Suggestion ]; } return []; } } class PaymentTypeSuggestionProvider implements SuggestionProviderInterface { public function getSuggestionsFor($data) { if (isset($data['customer']) && isset($data['contact']) && isset($data['workplace'])) { return [ ['payment-type a'], // Suggestion ['payment-type b'] // Suggestion ]; } return []; } } abstract class Colleague { /** * @var Mediator */ private $mediator; abstract public function receive($suggestions); public function send($message) { $this->getMediator()->send($message, $this); } public function setMediator(MediatorInterface $mediator) { $this->mediator = $mediator; } public function getMediator() { return $this->mediator; } } class Customer extends Colleague { public function receive($suggestions) {} public function __change() { $this->send(['customer' => 'Ocramius']); } } class Contact extends Colleague { public function receive($suggestions) { var_dump($suggestions); } } class Workplace extends Colleague { public function receive($suggestions) { var_dump($suggestions); } } class PaymentType extends Colleague { public function receive($suggestions) { var_dump($suggestions); } } class SuggestionMediator implements MediatorInterface { private $colleagues = []; private $suggestionProviders = []; public function addSuggestionProvider(SuggestionProviderInterface $provider) { $this->suggestionProviders[] = $provider; } public function add(Colleague $colleague) { $colleague->setMediator($this); $this->colleagues[] = $colleague; } public function send($message, Colleague $from) { foreach ($this->colleagues as $colleague) { // what I'll have here about providers? if ($from !== $colleague) { // $colleague->receive(...); } } } } $customer = new Customer(); $contact = new Contact(); $workplace = new Workplace(); $paymentType = new PaymentType(); $mediator = new SuggestionMediator(); $mediator->addSuggestionProvider(new ContactSuggestionProvider()); $mediator->addSuggestionProvider(new WorkplaceSuggestionProvider()); $mediator->addSuggestionProvider(new PaymentTypeSuggestionProvider()); $mediator->add($customer); $mediator->add($contact); $mediator->add($workplace); $mediator->add($paymentType); $customer->__change();
based on nVTZq
Output for 5.4.0 - 7.1.0
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/LV4oF on line 19
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/LV4oF on line 19
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING in /in/LV4oF on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING in /in/LV4oF on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/LV4oF on line 3
Process exited with code 255.