public static function instantiate($className, array $namedArguments)
    {
        $reflectionClass = new ReflectionClass($className);
        $parameters = $reflectionClass->getConstructor()->getParameters();

        $positionalDefaultFilledArguments = array();
        foreach ($parameters as $parameter) {
            // Do we have an argument set? If so use that.
            if (isset($namedArguments[$parameter->name])) {
                $positionalDefaultFilledArguments[] = $namedArguments[$parameter->name];
                continue;
            }

            // Otherwise use the default.
            $positionalDefaultFilledArguments[] = $parameter->getDefaultValue();
        }

        return $reflectionClass->newInstanceArgs($positionalDefaultFilledArguments);
    }
public static function instantiate($className, array $namedArguments)
    {
        $reflectionClass = new ReflectionClass($className);
        $parameters = $reflectionClass->getConstructor()->getParameters();

        $positionalDefaultFilledArguments = array();
        foreach ($parameters as $parameter) {
            // Do we have an argument set? If so use that.
            if (isset($namedArguments[$parameter->name])) {
                $positionalDefaultFilledArguments[] = $namedArguments[$parameter->name];
                continue;
            }

            // Otherwise use the default.
            $positionalDefaultFilledArguments[] = $parameter->getDefaultValue();
        }

        return $reflectionClass->newInstanceArgs($positionalDefaultFilledArguments);
    }
public static function instantiate ( $ className , array $ namedArguments ) { $ reflectionClass = new ReflectionClass ( $ className ) ; $ parameters = $ reflectionClass -> getConstructor ( ) -> getParameters ( ) ; $ positionalDefaultFilledArguments = array ( ) ; foreach ( $ parameters as $ parameter ) { // Do we have an argument set? If so use that. if ( isset ( $ namedArguments [ $ parameter -> name ] ) ) { $ positionalDefaultFilledArguments [ ] = $ namedArguments [ $ parameter -> name ] ; continue ; } // Otherwise use the default. $ positionalDefaultFilledArguments [ ] = $ parameter -> getDefaultValue ( ) ; } return $ reflectionClass -> newInstanceArgs ( $ positionalDefaultFilledArguments ) ; }
Instantiate a class with named arguments.

An answer to a problem that should not exist (legacy large Entities).

@param string $className
@param array $namedArguments
@return object
Instantiate a class with named arguments .