3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /** * Module creator */ Class Module { public function __construct($module) { $this->module = new System; } public function extensions($extension) { // In real life, we scan the $extension directory for class names // For now we hardcode a few here return [ 'SystemSettingsProfile', 'SystemSettingsPrivacy', 'SystemSettingsAccount', ]; } } /** * System service provider */ class System {} /** * System profile extensions */ abstract class Extension { public function render() { return get_called_class(); } public static function getTabs() { return get_called_class(); } } class SystemSettingsProfile extends Extension {} class SystemSettingsPrivacy extends Extension {} class SystemSettingsAccount extends Extension {} /** * Controller */ class Controller { public function index() { $extensions = (new Module('system'))->extensions('profile'); foreach($extensions as $extension) { $tabs[] = $extension::getTabs(); // Real-life: control structure checks if route key matches extension key // Except now we just keep overriding $content with the last $content = (new $extension)->render(); } var_dump($tabs, $content); } } (new Controller)->index();
Output for 5.4.0 - 7.1.0
array(3) { [0]=> string(21) "SystemSettingsProfile" [1]=> string(21) "SystemSettingsPrivacy" [2]=> string(21) "SystemSettingsAccount" } string(21) "SystemSettingsAccount"
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/1pRTr on line 17
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/1pRTr on line 17
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/1pRTr on line 8
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, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/1pRTr on line 8
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/1pRTr on line 8
Process exited with code 255.