3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Acd; /** * Registry class * * Simple class to store or get elements from configuration registry */ class Registry { /** @var array Registry configuration array */ private $data = []; /** * Class constructor * @param array $data List of values to add to the registry */ public function __construct(array $data = []) { if(!empty($data)) { foreach ($data as $key => $value) { $this->set($key, $value); } } } public function offsetSet($key, $value) { if (!$key) { $this->data[] = $value; } else { $this->data[$key] = $value; } } /** * Key to retrieve * * @param mixed $key * @return string|null */ public function offsetGet($key) { if (isset($this->data[$key])) { return $this->data[$key]; } return null; } /** * Whether a key exists * * @param mixed $key * @return bool */ public function offsetExists($key) { return isset($this->data[$key]); } /** * Key to unset * * @param mixed $key */ public function offsetUnset($key) { unset($this->data[$key]); } /** * Adds element to registry array * * @param string $key - registry Key * @param mixed $value - registry Value * @throws Exception When there is a duplicate $key */ public function set($key, $value) { if (isset($this->data[$key])) { throw new \Exception('There is already an entry for key: ' . $key); } $this->data[$key] = $value; } /** * Retrieves elements from registry array * * @param string $key * @return mixed returns a registry value * @throws Exception when no $key found */ public function get($key) { if (!isset($this->data[$key])) { throw new \Exception('There is no entry for key: ' . $key); } return $this->data[$key]; } /** * Remove an entry from the Registry * * @param string $key * @return void */ public function remove($key) { unset($this->data[$key]); } /** * Return true if value is empty for given key * * @param string $key * @return bool */ public function isEmpty($key) { return empty($this->data[$key]); } /** * Reset Registry container */ public function reset() { $this->data = []; } /** * Return total number of data elements * @return int */ public function count() { return count($this->data); } /** * IteratorAggregate interface required method * * @return \ArrayIterator */ public function getIterator() { return new \ArrayIterator($this->data); } } class Main { private $registry = null; private $service = null; private $obj = null; public function __construct() { if (!($this->registry instanceof Registry)) { $this->registry = new Registry; } } public function getService($service) { return $this->registry->get($service); } public function setService($class, array $args = null) { return $this->registry->set($class, function() use ($class, $args) { $class = $this->isValidService($class); return new $class($args); }); } private function isValidService($classname) { $classname = __NAMESPACE__ . '\\' . ucwords($classname); if(class_exists($classname)) { return $classname; } else { throw new \Exception("Invalid class name given: " . $classname); } } /** * Magic method to retrieve the Object * @param string $obj * @return object Object instance */ public function __get($obj) { $obj = $this->registry->get($obj); return $obj(); } } // Initialize main class $app = new Acd\Main(); // Start Request Service $app->setService('request');
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught Error: Class "Acd\Acd\Main" not found in /in/sa2So:214 Stack trace: #0 {main} thrown in /in/sa2So on line 214
Process exited with code 255.
Output for 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
Fatal error: Uncaught Error: Class 'Acd\Acd\Main' not found in /in/sa2So:214 Stack trace: #0 {main} thrown in /in/sa2So on line 214
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Fatal error: Class 'Acd\Acd\Main' not found in /in/sa2So on line 214
Process exited with code 255.

preferences:
262.38 ms | 402 KiB | 330 Q