/** * _create_object * Attempts to instantiate the requested class via any of the * commonly used instantiation methods employed throughout EE. * The priority for instantiation is as follows: * - abstract classes or any class flagged as "load only" (no instantiation occurs) * - model objects via their 'new_instance_from_db' method * - model objects via their 'new_instance' method * - "singleton" classes" via their 'instance' method * - standard instantiable classes via their __constructor * Prior to instantiation, if the classname exists in the dependency_map, * then the constructor for the requested class will be examined to determine * if any dependencies exist, and if they can be injected. * If so, then those classes will be added to the array of arguments passed to the constructor * * @access protected * @param string $class_name * @param array $arguments * @param string $type * @param bool $from_db * @return null | object * @throws \EE_Error */ protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) { $class_obj = null; // don't give up! you gotta... try { // create reflection $reflector = $this->get_ReflectionClass($class_name); // make sure arguments are an array $arguments = is_array($arguments) ? $arguments : array($arguments); // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, // else wrap it in an additional array so that it doesn't get split into multiple parameters $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) ? $arguments : array($arguments); // attempt to inject dependencies ? if ($this->_dependency_map->has($class_name)) { $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); } // instantiate the class and add to the LIB array for tracking // EE_Base_Classes are instantiated via new_instance by default (models call them via new_instance_from_db) if ($reflector->getConstructor() === null || $reflector->isAbstract()) { // no constructor = static methods only... nothing to instantiate, loading file was enough //$instantiation_mode = "no constructor"; $class_obj = true; } else { if ($from_db && method_exists($class_name, 'new_instance_from_db')) { //$instantiation_mode = "new_instance_from_db"; $class_obj = call_user_func_array(array($class_name, 'new_instance_from_db'), $arguments); } else { if (method_exists($class_name, 'new_instance')) { //$instantiation_mode = "new_instance"; $class_obj = call_user_func_array(array($class_name, 'new_instance'), $arguments); } else { if (method_exists($class_name, 'instance')) { //$instantiation_mode = "instance"; $class_obj = call_user_func_array(array($class_name, 'instance'), $arguments); } else { if ($reflector->isInstantiable()) { //$instantiation_mode = "isInstantiable"; $class_obj = $reflector->newInstanceArgs($arguments); } else { // heh ? something's not right ! //$instantiation_mode = 'none'; throw new EE_Error(sprintf(__('The %s file %s could not be instantiated.', 'event_espresso'), $type, $class_name)); } } } } } } catch (Exception $e) { if (!$e instanceof EE_Error) { $e = new EE_Error($e->getMessage()); } $e->get_error(); } return $class_obj; }