Create a class using an array of constructor arguments

@param string $className Class name
@param array  $args      Arguments for the class constructor

@return mixed
@throws RuntimeException
@deprecated
@codeCoverageIgnore
167948
private static function createObject($className, array $args = null)
    {
        try {
            if (!$args) {
                return new $className;
            } else {
                $c = new \ReflectionClass($className);
                return $c->newInstanceArgs($args);
            }
        } catch (\Exception $e) {
            throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
        }
    }
473835
public function createEvent($eventName, $class, array $args)
    {
        if ( ! is_string($eventName)) {
            $exception = array(
                'message' => '"exception_invalid_argument_provided_for_event_name',
                'parameters' => array(
                    '%className%' => get_class($this),
                ),
            );
            throw new InvalidArgumentException(json_encode($exception));
        }

        if ( ! class_exists($class)) {
            $exception = array(
                'message' => 'exception_invalid_class_name_for_createEvent',
                'parameters' => array(
                    '%argumentClass%' => get_class($this),
                    '%className%' => get_class($this),
                ),
            );
            throw new InvalidArgumentException(json_encode($exception));
        }

        // When the event already exists, it is recreated
        $event = $this->fetchEvent($eventName);
        if (null !== $event) {unset($this->events[$eventName]);}

        $event = new $class();
        if (! $event instanceof Event) {
            $exception = array(
                'message' => 'exception_invalid_class_instance_for_createEvent',
                'parameters' => array(
                    '%argumentClass%' => get_class($this),
                    '%className%' => get_class($this),
                ),
            );
            throw new InvalidArgumentTypeException(json_encode($exception));
        }

        $this->events[$eventName] = $event;

        $numberOfArgs = count($args);
        if ($numberOfArgs == 0) return $this;

        $methods = array_slice($this->methods, 0, $numberOfArgs);
        $callables = array_combine($methods, $args);

        // Valorizes the event's methods
        foreach ($callables as $method => $arg) {
            call_user_func(array($event, $method), $arg);
        }

        return $this;
    }
1317254
private function setClass( $name, $args ) {

		// Build the class string dynamically.
		$class = __NAMESPACE__ . '\\Notice\\' . ucfirst( $name );

		// Create a new instance or add our filters.
		return $this->class = class_exists( $class ) ? new $class( $args ) : Filter::add( $this );
	}
1187599
public static function instance($className = __CLASS__)
    {
        if (!isset(self::$instances[$className])) {
            self::$instances[$className] = new $className;
        }

        return self::$instances[$className];
    }
1069044
static function instance($class, array $args = []) {
    // Get class name
    $class = static::getClass($class);
    // Count args
    switch (count($args)) {
      case 1:   return new $class($args[0]);
      case 2:   return new $class($args[0], $args[1]);
      case 3:   return new $class($args[0], $args[1], $args[2]);
      case 4:   return new $class($args[0], $args[1], $args[2], $args[3]);
      case 5:   return new $class($args[0], $args[1], $args[2], $args[3], $args[4]);
      case 6:   return new $class($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]);
      case 7:   return new $class($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6]);
      case 8:   return new $class($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6], $args[7]);
      case 9:   return new $class($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6], $args[7], $args[8]);
      case 10:  return new $class($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6], $args[7], $args[8], $args[9]);
    }
    // Return without args
    return new $class();
  }
814054
public function createClass($classname)
    {
        // Check for possible overwrite.
        if (isset($this->rewrite[$classname]) === true) {
            $classname = $this->rewrite[$classname];
        }

        return new $classname($this);
    }
1069165
public static function newInstance($baseclass, $args = array(), &$module = null)
    {
        $modules = ModuleManager::getInstance()->getModules();
        foreach ($modules as $module) {
            if (in_array($baseclass, $module->getModels())) {
                $classes = array('\\' . $module->modelNamespace . '\\' . $baseclass, $baseclass);
                foreach ($classes as $class) {
                    if (class_exists($class)) {

                        return new $class($args);
                    }
                }
            }
        }
        $class = '\\GLFramework\\Model\\' . $baseclass;
        if (class_exists($class)) {
            return new $class($args);
        }
        return false;
    }
1240655
protected static function checkInternalClass($instance, $class, array $args = [])
    {
        if (null !== $instance) {
            return;
        }

        $messages = [];
        foreach ($args as $name => $value) {
            $messages[] = sprintf('%s => %s', $name, $value);
        }

        throw new \RuntimeException(sprintf(
            'Unable to create internal class: %s, with params: %s',
            $class,
            implode(', ', $messages)
        ));
    }
394286
public static function bindStatic($className, $methodName, $args = null)
    {
        if (empty($args)) {
            $args = null;
        }
        $native = [$className, $methodName];
        $callback = function () use ($native, $args) {
            $a = func_get_args();
            if ($args !== null) {
                $a = array_merge($a, $args);
            }
            return call_user_func_array($native, $a);
        };
        return $callback->bindTo(null, $className);
    }
772191
public static function constructClassInstance( $clazz, $args ) {
		// $args should be a non-associative array; show nice error if that's not the case
		if ( $args && array_keys( $args ) !== range( 0, count( $args ) - 1 ) ) {
			throw new InvalidArgumentException( __METHOD__ . ': $args cannot be an associative array' );
		}

		return new $clazz( ...$args );
	}
1269055
public function create(string $serviceName, array $args = [])
    {
        if (!$this->registry->has($serviceName)) {
            throw new \LogicException(sprintf('The service "%s" is not registered.', $serviceName));
        }

        $classOrCallable = $this->registry->get($serviceName);

        if (is_callable($classOrCallable)) {
            return call_user_func($classOrCallable);
        }

        if (is_subclass_of($classOrCallable, HttpService::class)) {
            return new $classOrCallable($this->httpClient, $this->requestFactory, $args);
        }

        $r = new \ReflectionClass($classOrCallable);

        return $r->newInstanceArgs($args);
    }
654430
public function createFormatter($classOrName, $args = [])
    {
        $classes = [
            $classOrName,
            '\\Monolog\\Formatter\\' . $classOrName
        ];

        foreach ($classes as $class)
        {
            if (class_exists($class))
            {
                $object = (new ReflectionClass($class))->newInstanceArgs($args);

                return new Formatter($object);
            }
        }

        throw new InvalidArgumentException("Monolog formatter [$classOrName] does not exist.");
    }
613321
public function build($class, ...$arguments)
    {
        if (is_object($class)) {
            return $class;
        }

        if (!class_exists($class)) {
            throw new InvalidArgumentException("Unable to build class: $class it does not exist");
        }

        $reflection = new ReflectionClass($class);
        $this->log(LogLevel::DEBUG, "Building class: {class}", ['class' => $class]);
        $object = $reflection->newInstanceArgs($arguments);

        if ($object instanceof BuilderAwareInterface) {
            $object->setBuilder($this);
        }

        if ($this->logger && ($object instanceof LoggerAwareInterface)) {
            $object->setLogger($this->logger);
        }

        return $object;
    }
5831
public function createNew($className)
    {
        $constructArguments = func_get_args();
        $object = $this->_createNew( ...$constructArguments);
        
        $traits = class_uses($object);

        if (isset($traits[Framework::class]) == true) {
            $object->setContainer($this->container);
        }

        return $object;
    }
326470
protected static function createInstance(array $args = [])
    {
        if (!self::$reflection) {
            self::$reflection = new \ReflectionClass('IntlDateFormatter');
        }

        $instance = self::$reflection->newInstanceArgs($args);

        self::checkInternalClass($instance, '\IntlDateFormatter', $args);

        return $instance;
    }
431114
public static function create($className) {
        // The best logger configuration for the given class name
        $bestLoggerConfiguration = LoggerManager::findBestLoggerConfigurationForClass($className);

        // If no logger configuration was found for the given class name
        if (!isset($bestLoggerConfiguration)) {
            // Return an empty logger
            return new Logger($className);
        }

        // A logger configuration has been found for the given class name
        $logger = new Logger($className);
        $logger->_targetClassName = $bestLoggerConfiguration['class'];
        $logger->_level = LogLevel::createFromCode($bestLoggerConfiguration['level']);
        $logger->_setupWriters($bestLoggerConfiguration['writers']);

        return $logger;
    }
323271
public static function make($namespace,$args=[]) {
        $reflect = new \ReflectionClass($namespace);
        $constructor = $reflect->getConstructor();
        if (!$constructor || $constructor->getNumberOfParameters() == 0) {
            $constructor = $reflect->newInstance();
        }
        else {
            $constructor = $reflect->newInstanceArgs($args);
        }
        return $constructor;
    }