3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace App\Com; class Test { protected static $_instance; public static function getInstance() { if(!self::$_instance) { self::$_instance = new self(); } return self::$_instance; } public function test() { return 'works'; } protected function __construct() { } } class Service { protected static $_instance; protected $_connection; protected $_case = []; public static function getInstance() { if(!self::$_instance) { self::$_instance = new self(); } return self::$_instance; } protected function __construct() { $this->_case[] = Test::getInstance(); // TODO: Establish entity to database } public function using($nsp) { foreach($this->_case as $obj) { if($nsp instanceof $obj) { return $obj::getInstance(); } } throw new \Exception("Call to $nsp did not match any libraries."); } protected function __clone() {} } echo Service::getInstance()->using('\App\Com\Test')->test();
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 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.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Fatal error: Uncaught Exception: Call to \App\Com\Test did not match any libraries. in /in/knhjm:58 Stack trace: #0 /in/knhjm(64): App\Com\Service->using('\\App\\Com\\Test') #1 {main} thrown in /in/knhjm on line 58
Process exited with code 255.
Output for 5.6.0 - 5.6.29
Fatal error: Uncaught exception 'Exception' with message 'Call to \App\Com\Test did not match any libraries.' in /in/knhjm:58 Stack trace: #0 /in/knhjm(64): App\Com\Service->using('\\App\\Com\\Test') #1 {main} thrown in /in/knhjm on line 58
Process exited with code 255.

preferences:
147.81 ms | 407 KiB | 5 Q