3v4l.org

run code in 300+ PHP versions simultaneously
<?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();
Output for git.master, git.master_jit, rfc.property-hooks

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
38.93 ms | 401 KiB | 8 Q