3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace app; define('YII_ENV_PROD', false); class BaseObject { protected $enableFriendClassesInProd = false; protected $calledClassStackLimit = 0; protected $friendParents = false; protected $friendDescendants = false; protected $friendClasses = []; public function __call($name, $args) { if (method_exists($this, $name) && method_exists($this, '_call')) { if (!YII_ENV_PROD || $enableFriendClassesInProd ) { $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $this->calledClassStackLimit); $stackClasses = array_column($backtrace, 'class'); $calledClass = array_pop($stackClasses); if (in_array($calledClass, $this->friendClasses)) { return call_user_func_array([$this, '_call'], [$name, $args]); } elseif ($this->friendDescendants && is_subclass_of($calledClass, static::class)) { return call_user_func_array([$this, '_call'], [$name, $args]); } elseif ($this->friendParents && is_subclass_of(static::class, $calledClass)) { return call_user_func_array([$this, '_call'], [$name, $args]); } else { throw new \Exception('Method disallowed: ' . get_class($this) . '::' . $name); } } return call_user_func_array([$this, '_call'], [$name, $args]); } else { throw new \Exception('Calling unknown method:' . get_class($this) . '::' . $name); } } public function __get($name) { $getter = 'get' . $name; if (method_exists($this, $getter)) { return $this->$getter(); } elseif (method_exists($this, 'set' . $name)) { throw new \Exception('Getting write-only property: ' . get_class($this) . '::' . $name); } throw new \Exception('Getting unknown property: ' . get_class($this) . '::' . $name); } public function __set($name, $value) { $setter = 'set' . $name; if (method_exists($this, $setter)) { $this->$setter($value); } elseif (method_exists($this, 'get' . $name)) { throw new \Exception('Setting read-only property: ' . get_class($this) . '::' . $name); } else { throw new \Exception('Setting unknown property: ' . get_class($this) . '::' . $name); } } } class Yii { public static $app; } class Module extends BaseObject { protected $modules; private function setInstance() { Yii::$app->addLoadedModule($this); } public static function getInstance() { $loadedModules = Yii::$app->getLoadedModules(); if (isset($loadedModules[static::class])) { return $loadedModules[static::class]; } return null; } public function getModule($name) { if (isset($this->modules[$name])) { if (is_object($this->modules[$name])) { return $this->modules[$name]; } else { $class = $this->modules[$name]; $module = new $class(); $module->setInstance(); return $module; } } } } class Application extends Module { private $loadedModules = []; protected $calledClassStackLimit = 10; protected $friendParents = true; protected $friendClasses = ['app\ModuleLocator']; public function __construct($config) { if (isset($config['modules'])) { $this->modules = $config['modules']; } } private function addLoadedModule($object) { $this->loadedModules[get_class($object)] = $object; } private function getLoadedModules() { return $this->loadedModules; } protected function _call($name, $args) { return call_user_func_array([$this, $name], $args); } } class MyFirstModule extends Module { public function whoAmI() { echo "\n" . 'I am the first module'; } } class MySecondModule extends Module { public function WhoAmI() { echo "\n" . 'I am the second module'; } } class MyThirdModule extends Module { public function WhoAmI() { echo "\n" . 'I am the third module'; } } class ModuleLocator extends BaseObject { public static function getLoadedModules() { return Yii::$app->loadedModules; } } $config = [ 'modules' => [ 'myFirstModule' => 'app\MyFirstModule', 'mySecondModule' => 'app\MySecondModule', 'myThirdModule' => 'app\MyThirdModule', ] ]; Yii::$app = new Application($config); $myFirstModule = Yii::$app->getModule('myFirstModule'); $myFirstModule->whoAmI(); $mySecondModule = Yii::$app->getModule('mySecondModule'); $mySecondModule->whoAmI(); echo "\n"; print_r(ModuleLocator::getLoadedModules()); echo "\n"; $myFirstModuleInstanse = MyFirstModule::getInstance(); $myFirstModuleInstanse->whoAmI(); $mySecondModuleInstanse = MySecondModule::getInstance(); $mySecondModuleInstanse->whoAmI();
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
I am the first module I am the second module Array ( [app\MyFirstModule] => app\MyFirstModule Object ( [enableFriendClassesInProd:protected] => [calledClassStackLimit:protected] => 0 [friendParents:protected] => [friendDescendants:protected] => [friendClasses:protected] => Array ( ) [modules:protected] => ) [app\MySecondModule] => app\MySecondModule Object ( [enableFriendClassesInProd:protected] => [calledClassStackLimit:protected] => 0 [friendParents:protected] => [friendDescendants:protected] => [friendClasses:protected] => Array ( ) [modules:protected] => ) ) I am the first module I am the second module
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 5.5.0 - 5.5.38, 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
I am the first module I am the second module Array ( [app\MyFirstModule] => app\MyFirstModule Object ( [modules:protected] => [enableFriendClassesInProd:protected] => [calledClassStackLimit:protected] => 0 [friendParents:protected] => [friendDescendants:protected] => [friendClasses:protected] => Array ( ) ) [app\MySecondModule] => app\MySecondModule Object ( [modules:protected] => [enableFriendClassesInProd:protected] => [calledClassStackLimit:protected] => 0 [friendParents:protected] => [friendDescendants:protected] => [friendClasses:protected] => Array ( ) ) ) I am the first module I am the second module
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /in/kAHU6 on line 29
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/kAHU6 on line 17
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/kAHU6 on line 3
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, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/kAHU6 on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/kAHU6 on line 3
Process exited with code 255.

preferences:
214.69 ms | 417 KiB | 5 Q