3v4l.org

run code in 300+ PHP versions simultaneously
<?php class BookMediator { private $authorObject; private $titleObject; function __construct() { $this->authorObject = new BookAuthorColleague($this); $this->titleObject = new BookTitleColleague($this); } function getAuthor() {return $this->authorObject;} function getTitle() {return $this->titleObject;} // when title or author change case, this makes sure the other // stays in sync function change(BookColleague $changingClassIn) { if ($changingClassIn instanceof BookAuthorColleague) { if ('upper' == $changingClassIn->getState()) { if ('upper' != $this->getTitle()->getState()) { $this->getTitle()->setTitleUpperCase($this->getAuthor()->getAuthor()); } } elseif ('lower' == $changingClassIn->getState()) { if ('lower' != $this->getTitle()->getState()) { $this->getTitle()->setTitleLowerCase($this->getAuthor()->getAuthor()); } } } elseif ($changingClassIn instanceof BookTitleColleague) { if ('upper' == $changingClassIn->getState()) { if ('upper' != $this->getAuthor()->getState()) { $this->getAuthor()->setAuthorUpperCase($this->getTitle()->getTitle()); } } elseif ('lower' == $changingClassIn->getState()) { if ('lower' != $this->getAuthor()->getState()) { $this->getAuthor()->setAuthorLowerCase($this->getTitle()->getTitle()); } } } } } abstract class BookColleague { private $mediator; function __construct($mediator_in) { $this->mediator = $mediator_in; } function getMediator() {return $this->mediator;} function changed($changingClassIn) { getMediator()->titleChanged($changingClassIn); } } class BookAuthorColleague extends BookColleague { private $author; private $state; function __construct($mediator_in) { parent::__construct($mediator_in); } function getAuthor() {return $this->author;} function setAuthor($author_in) {$this->author = $author_in;} function getState() {return $this->state;} function setState($state_in) {$this->state = $state_in;} function setAuthorUpperCase($message) { $this->setAuthor(strtoupper($message)); $this->setState('upper'); $this->getMediator()->change($this); } function setAuthorLowerCase($message) { $this->setAuthor(strtolower($message)); $this->setState('lower'); $this->getMediator()->change($this); } } class BookTitleColleague extends BookColleague { private $title; private $state; function __construct($mediator_in) { parent::__construct($mediator_in); } function getTitle() {return $this->title;} function setTitle($title_in) {$this->title = $title_in;} function getState() {return $this->state;} function setState($state_in) {$this->state = $state_in;} function setTitleUpperCase($message) { $this->setTitle(strtoupper($message)); $this->setState('upper'); $this->getMediator()->change($this); } function setTitleLowerCase($message) { $this->setTitle(strtolower($message)); $this->setState('lower'); $this->getMediator()->change($this); } } $mediator = new BookMediator(); $author = $mediator->getAuthor(); $title = $mediator->getTitle(); $author->setAuthorLowerCase("hallo"); writeln($author->getAuthor()); writeln($title->getTitle()); $title->setTitleUpperCase("huhu"); writeln($author->getAuthor()); writeln($title->getTitle()); $author->setAuthorUpperCase("huhggu"); writeln($author->getAuthor()); writeln($title->getTitle()); function writeln($line_in) { echo $line_in.'<br/>'; }
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
hallo<br/>hallo<br/>HUHU<br/>HUHU<br/>HUHGGU<br/>HUHU<br/>
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 hallo<br/>hallo<br/>HUHU<br/>HUHU<br/>HUHGGU<br/>HUHU<br/>

preferences:
237.57 ms | 402 KiB | 290 Q