public static function get(string $class, array $config = [])
    {
        $reflection = new \ReflectionClass($class);

        if (!$reflection->isInstantiable()) {
            throw new \ReflectionException(sprintf('`%s` is not instantiable.', $reflection->name));
        }
        if ($reflection->implementsInterface(IConfigurable::class)) {
            // set $config as the last parameter (existing one will be overwritten)
            return $reflection->newInstanceArgs([$config]);
        }

        $object = $reflection->newInstance();
        static::configure($object, $config);

        return $object;
    }
public static function get(string $class, array $config = [])
    {
        $reflection = new \ReflectionClass($class);

        if (!$reflection->isInstantiable()) {
            throw new \ReflectionException(sprintf('`%s` is not instantiable.', $reflection->name));
        }
        if ($reflection->implementsInterface(IConfigurable::class)) {
            // set $config as the last parameter (existing one will be overwritten)
            return $reflection->newInstanceArgs([$config]);
        }

        $object = $reflection->newInstance();
        static::configure($object, $config);

        return $object;
    }
public static function get ( string $ class , array $ config = [ ] ) { $ reflection = new \ ReflectionClass ( $ class ) ; if ( ! $ reflection -> isInstantiable ( ) ) { throw new \ ReflectionException ( sprintf ( '`%s` is not instantiable.' , $ reflection -> name ) ) ; } if ( $ reflection -> implementsInterface ( IConfigurable :: class ) ) { // set $config as the last parameter (existing one will be overwritten) return $ reflection -> newInstanceArgs ( [ $ config ] ) ; } $ object = $ reflection -> newInstance ( ) ; static :: configure ( $ object , $ config ) ; return $ object ; }
Creates an instance of the specified class. Inspired by Yii2 di.
@see \yii\di\Container::build
@param string $class the class name
@param array $config configurations to be applied to the new instance
@return mixed
@throws \ReflectionException
@throws InvalidConfigException
Creates an instance of the specified class . Inspired by Yii2 di .