public function &createInstance(__InstanceDefinition &$instance_definition)
 {
     $return_value = null;
     //Create an empty instance:
     $instance_class = $instance_definition->getClass();
     $factory_instance_id = $instance_definition->getFactoryInstanceId();
     $factory_method = $instance_definition->getFactoryMethod();
     $constructor_arguments_def = $instance_definition->getConstructorArguments()->toArray();
     $constructor_arguments = array();
     foreach ($constructor_arguments_def as $constructor_argument_def) {
         $constructor_arguments[] = $this->_getInstanceFromPropertyValue($constructor_argument_def);
     }
     if ($factory_instance_id != null && $instance_class != null) {
         throw __ExceptionFactory::getInstance()->createException('ERR_AMBIGUOUS_CREATION_METHOD', array($instance_class, $factory_instance_id));
     } else {
         if ($instance_class != null) {
             if ($factory_method == null) {
                 if (count($constructor_arguments) > 0) {
                     $reflection_obj = new ReflectionClass($instance_class);
                     $return_value = $reflection_obj->newInstanceArgs($constructor_arguments);
                 } else {
                     $return_value = new $instance_class();
                 }
             } else {
                 $return_value = call_user_func_array(array($instance_class, $factory_method), $constructor_arguments);
             }
         } else {
             $factory_instance = $this->_context->getContextInstance($factory_instance_id);
             if ($factory_instance != null && $factory_method != null) {
                 $return_value = call_user_func_array(array($factory_instance, $factory_method), $constructor_arguments);
             } else {
                 if ($factory_instance == null) {
                     throw __ExceptionFactory::getInstance()->createException('ERR_WRONG_FACTORY_SPECIFICATION', array($factory_instance_id, $factory_method));
                 } else {
                     throw __ExceptionFactory::getInstance()->createException('ERR_FACTORY_METHOD_REQUIRED', array($factory_instance_id));
                 }
             }
         }
     }
     if ($return_value != null) {
         $properties = $instance_definition->getProperties();
         $this->injectProperties($properties, $return_value);
     }
     //Execute startup method if any:
     $startup_method = $instance_definition->getStartupMethod();
     if ($startup_method != null && method_exists($return_value, $startup_method)) {
         $return_value->{$startup_method}();
     }
     return $return_value;
 }
 private function _parseConstructorArgument(__ConfigurationSection $property, __InstanceDefinition &$instance_definition)
 {
     if ($property->hasAttribute('index')) {
         $argument_index = $property->getAttribute('index');
     } else {
         $argument_index = null;
     }
     $argument_value = $this->_parsePropertyValue($property, 'constructor argument', $instance_definition->getId());
     $instance_definition->addConstructorArgument($argument_value, $argument_index);
 }