3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /** * Object Registry: get instance of requested and optionally registered object. * * Object (instance of a class) is generater, or returned from internal cache * if it was requested and instantiated before. * * @author Time.ly Network, Inc. * @since 2.0 * @package Ai1EC * @subpackage Ai1EC.Registry */ class Ai1ec_Object_Registry { /** * @var array The internal objects cache */ private $_objects = array(); /** * @var Ai1ec_Loader The Ai1ec_Loader instance used by the registry */ private $_loader = null; /** * Get class instance. * * Return an instance for the requested key, this method has an internal * cache. * * @param string $key Name of previously registered object or parseable * class name * * @return object Instance of the requested class */ public function get( $key ) { $class_data = $this->_loader->resolve_class_name( $key ); if ( ! $class_data ) { throw new Ai1ec_Bootstrap_Exception( 'Unable to resolve class for "' . $key . '"' ); } $class_name = $class_data['c']; $instantiator = $class_data['i']; $args = array_slice( func_get_args(), 1 ); if ( isset ( $class_data['r'] ) ) { array_unshift( $args, $this ); } if ( Ai1ec_Loader::NEWINST === $instantiator ) { return $this->initiate( $class_name, $args ); } if ( Ai1ec_Loader::GLOBALINST === $instantiator ) { if ( ! isset( $this->_objects[$class_name] ) ) { // Ask the loader to load the required files to avoid autoloader $this->_loader->load( $class_name ); $this->_objects[$class_name] = $this->initiate( $class_name, $args ); } return $this->_objects[$class_name]; } // Ok it's a factory. $factory = explode( '.', $instantiator ); return $this->dispatch( $factory[0], $factory[1], $args ); } /** * Instanciate the class given the class names and arguments. * * @param string $class_name The name of the class to instanciate. * @param array $argv An array of aguments for construction. * * @return object A new instance of the requested class */ public function initiate( $class_name, array $argv = array() ) { switch ( count( $argv ) ) { case 0: return new $class_name(); case 1: return new $class_name( $argv[0] ); case 2: return new $class_name( $argv[0], $argv[1] ); case 3: return new $class_name( $argv[0], $argv[1], $argv[2] ); case 4: return new $class_name( $argv[0], $argv[1], $argv[2], $argv[3] ); case 5: return new $class_name( $argv[0], $argv[1], $argv[2], $argv[3], $argv[4] ); } $reflected = new ReflectionClass( $class_name ); return $reflected->newInstanceArgs( $argv ); } /** * A call_user_func_array alternative. * * @param string $class * @param string $method * @param array $params * * @return mixed */ public function dispatch( $class, $method, $params = array() ) { if ( empty( $class ) ) { switch ( count( $params) ) { case 0: return $method(); case 1: return $method( $params[0] ); case 2: return $method( $params[0], $params[1] ); case 3: return $method( $params[0], $params[1], $params[2] ); default: return call_user_func_array( array( $class, $method ), $params ); } } else { // get an instance of the class $class = $this->get( $class ); switch ( count( $params) ) { case 0: return $class->{$method}(); case 1: return $class->{$method}( $params[0] ); case 2: return $class->{$method}( $params[0], $params[1] ); case 3: return $class->{$method}( $params[0], $params[1], $params[2] ); default: return call_user_func_array( array( $class, $method ), $params ); } } } /** * Constructor * * Initialize the Registry * * @param Ai1ec_Loader $ai1ec_loader Instance of Ai1EC classes loader * * @return void Constructor does not return */ public function __construct( $ai1ec_loader ) { $this->_loader = $ai1ec_loader; } } class Ai1ec_Core_Callback { protected $_registry = null; protected $_registry_name = null; protected $_method = null; public function __construct( Ai1ec_Objects_Registry $registry, $path, $method ) { $this->_registry = $registry; $this->_registry_name = $path; $this->_method = $method; } public function execute( array $argv ) { return $this->_registry->get( $this->_registry_name )->{$this->_method}( $argv ); } } $registry = new Ai1ec_Object_Registry(null); for($i = 0; $i < 200; $i++){ $obj = new Ai1ec_Core_Callback($registry, "bla bla bla", "method name"); }
Output for 7.0.0 - 7.1.0
Fatal error: Uncaught TypeError: Argument 1 passed to Ai1ec_Core_Callback::__construct() must be an instance of Ai1ec_Objects_Registry, instance of Ai1ec_Object_Registry given, called in /in/n5aFu on line 190 and defined in /in/n5aFu:176 Stack trace: #0 /in/n5aFu(190): Ai1ec_Core_Callback->__construct(Object(Ai1ec_Object_Registry), 'bla bla bla', 'method name') #1 {main} thrown in /in/n5aFu on line 176
Process exited with code 255.
Output for hhvm-3.12.0 - 3.13.2
Fatal error: Uncaught TypeError: Argument 1 passed to Ai1ec_Core_Callback::__construct() must be an instance of Ai1ec_Objects_Registry, Ai1ec_Object_Registry given in /in/n5aFu:180 Stack trace: #0 /in/n5aFu(190): Ai1ec_Core_Callback->__construct() #1 {main}
Process exited with code 255.
Output for hhvm-3.10.0
Catchable fatal error: Argument 1 passed to Ai1ec_Core_Callback::__construct() must be an instance of Ai1ec_Objects_Registry, Ai1ec_Object_Registry given in /in/n5aFu on line 180
Process exited with code 255.
Output for 5.2.0 - 5.6.28
Catchable fatal error: Argument 1 passed to Ai1ec_Core_Callback::__construct() must be an instance of Ai1ec_Objects_Registry, instance of Ai1ec_Object_Registry given, called in /in/n5aFu on line 190 and defined in /in/n5aFu on line 176
Process exited with code 255.
Output for 5.1.0 - 5.1.6
Fatal error: Class 'Ai1ec_Objects_Registry' not found in /in/n5aFu on line 176
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/n5aFu on line 84
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/n5aFu on line 19
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/n5aFu on line 19
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/n5aFu on line 19
Process exited with code 255.