3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** Object responsible for echoing debug data **/ class EchoLogger implements \SplObserver { public function update(\SplSubject $subject) { printf("Rand: %s, Status: %s", $subject->getRand(), $subject->getStatus()); } } /** Object responsible for emailing debug data **/ class EmailLogger implements \SplObserver { public function update(\SplSubject $subject) { /** Mail your data **/ } } class HttpClientService implements SplSubject { private $observers = array(); public function getRand() { return $this->rand; } public function getStatus() { return $this->status; } public function attach(\SplObserver $observer) { $this->observers[] = $observer; } public function detach(\SplObserver $observer) { if($key = array_search($observer,$this->observers, true)) { unset($this->observers[$key]); } } public function notify() { foreach ($this->observers as $observer) { $observer->update($this); } } public function getServiceStatus() { // -- SNIP -- $this->rand = rand(0, 10); $this->status = ($this->rand >= 5) ? 'success' : 'error'; // -- Snip -- return $this->status; } } $service = new HttpClientService; $service->attach(new EchoLogger); var_dump($service); $service->getServiceStatus();
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.7
object(HttpClientService)#1 (1) { ["observers":"HttpClientService":private]=> array(1) { [0]=> object(EchoLogger)#2 (0) { } } }
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 4 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 6 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 13 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 15 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 35 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 40 object(HttpClientService)#1 (1) { ["observers:private"]=> array(1) { [0]=> object(EchoLogger)#2 (0) { } } }
Output for 5.0.5
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 4 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 6 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 13 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 15 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 35 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 40 Fatal error: Interface 'SplObserver' not found in /in/gB4oE on line 4
Process exited with code 255.
Output for 5.0.0 - 5.0.4
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 4 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 6 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 13 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 15 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 35 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/gB4oE on line 40 Fatal error: Class 'SplObserver' not found in /in/gB4oE on line 4
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/gB4oE on line 4
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting '{' in /in/gB4oE on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/gB4oE on line 4
Process exited with code 255.

preferences:
157.24 ms | 401 KiB | 217 Q