src/Helpers/Container.php
public function getInstance($class, $subNamespace, array $constructorArgs = array(), $newInstance = false)
    {
        if (!isset($this->instances[$class]) || $newInstance) {
            // Try custom namespace.
            if (!empty($this->customNamespace)) {
                $fqClass = $this->tryNsInstance($class, $subNamespace, $this->customNamespace);
            }

            // Try the namespace passed to the constructor and any parent
            // namespaces, but stop at Acumulus.
            $namespaces = explode('\\', $this->shopNamespace);
            while (empty($fqClass) && !empty($namespaces)) {
                if (end($namespaces) === 'Acumulus') {
                    // Base level is always \Siel\Acumulus, even if
                    // \MyVendorName\Acumulus\MyWebShop was set as shopNamespace.
                    $namespace = static::baseNamespace;
                    $namespaces = array();
                } else {
                    $namespace = implode('\\', $namespaces);
                    array_pop($namespaces);
                }
                $fqClass = $this->tryNsInstance($class, $subNamespace, $namespace);
            }

            if (empty($fqClass)) {
                throw new \InvalidArgumentException("Class $class not found in namespace $subNamespace");
            }

            // Create a new instance.
            // As PHP5.3 produces a fatal error when a class has no constructor
            // and newInstanceArgs() is called, we have to differentiate between
            // no arguments and arguments.
            if (empty($constructorArgs)) {
                $this->instances[$class] = new $fqClass();
            } else {
                /** @noinspection PhpUnhandledExceptionInspection */
                $reflector = new ReflectionClass($fqClass);
                $this->instances[$class] = $reflector->newInstanceArgs($constructorArgs);
            }
        }
        return $this->instances[$class];
    }
public function getInstance($class, $subNamespace, array $constructorArgs = array(), $newInstance = false)
    {
        if (!isset($this->instances[$class]) || $newInstance) {
            // Try custom namespace.
            if (!empty($this->customNamespace)) {
                $fqClass = $this->tryNsInstance($class, $subNamespace, $this->customNamespace);
            }

            // Try the namespace passed to the constructor and any parent
            // namespaces, but stop at Acumulus.
            $namespaces = explode('\\', $this->shopNamespace);
            while (empty($fqClass) && !empty($namespaces)) {
                if (end($namespaces) === 'Acumulus') {
                    // Base level is always \Siel\Acumulus, even if
                    // \MyVendorName\Acumulus\MyWebShop was set as shopNamespace.
                    $namespace = static::baseNamespace;
                    $namespaces = array();
                } else {
                    $namespace = implode('\\', $namespaces);
                    array_pop($namespaces);
                }
                $fqClass = $this->tryNsInstance($class, $subNamespace, $namespace);
            }

            if (empty($fqClass)) {
                throw new \InvalidArgumentException("Class $class not found in namespace $subNamespace");
            }

            // Create a new instance.
            // As PHP5.3 produces a fatal error when a class has no constructor
            // and newInstanceArgs() is called, we have to differentiate between
            // no arguments and arguments.
            if (empty($constructorArgs)) {
                $this->instances[$class] = new $fqClass();
            } else {
                /** @noinspection PhpUnhandledExceptionInspection */
                $reflector = new ReflectionClass($fqClass);
                $this->instances[$class] = $reflector->newInstanceArgs($constructorArgs);
            }
        }
        return $this->instances[$class];
    }
public function getInstance ( $ class , $ subNamespace , array $ constructorArgs = array ( ) , $ newInstance = false ) { if ( ! isset ( $ this -> instances [ $ class ] ) || $ newInstance ) { // Try custom namespace. if ( ! empty ( $ this -> customNamespace ) ) { $ fqClass = $ this -> tryNsInstance ( $ class , $ subNamespace , $ this -> customNamespace ) ; } // Try the namespace passed to the constructor and any parent // namespaces, but stop at Acumulus. $ namespaces = explode ( '\\' , $ this -> shopNamespace ) ; while ( empty ( $ fqClass ) && ! empty ( $ namespaces ) ) { if ( end ( $ namespaces ) === 'Acumulus' ) { // Base level is always \Siel\Acumulus, even if // \MyVendorName\Acumulus\MyWebShop was set as shopNamespace. $ namespace = static :: baseNamespace ; $ namespaces = array ( ) ; } else { $ namespace = implode ( '\\' , $ namespaces ) ; array_pop ( $ namespaces ) ; } $ fqClass = $ this -> tryNsInstance ( $ class , $ subNamespace , $ namespace ) ; } if ( empty ( $ fqClass ) ) { throw new \ InvalidArgumentException ( "Class $class not found in namespace $subNamespace" ) ; } // Create a new instance. // As PHP5.3 produces a fatal error when a class has no constructor // and newInstanceArgs() is called, we have to differentiate between // no arguments and arguments. if ( empty ( $ constructorArgs ) ) { $ this -> instances [ $ class ] = new $ fqClass ( ) ; } else { /** @noinspection PhpUnhandledExceptionInspection */ $ reflector = new ReflectionClass ( $ fqClass ) ; $ this -> instances [ $ class ] = $ reflector -> newInstanceArgs ( $ constructorArgs ) ; } } return $ this -> instances [ $ class ] ; }
Returns an instance of the given class.

This method should normally be avoided, use the get{Class}() methods as
they know (and hide) what arguments to inject into the constructor.

The class is looked for in multiple namespaces, starting with the
$customNameSpace properties, continuing with the $shopNamespace property
and finally the base namespace (\Siel\Acumulus).

Normally, only 1 instance is created per class but the $newInstance
argument can be used to change this behavior.

@param string $class
The name of the class without namespace. The class is searched for in
multiple namespaces, see above.
@param string $subNamespace
The sub namespace (within the namespaces tried) in which the class
resides.
@param array $constructorArgs
A list of arguments to pass to the constructor, may be an empty array.
@param bool $newInstance
Whether to create a new instance (true) or reuse an already existing
instance (false, default)

@return object

@throws \InvalidArgumentException
Returns an instance of the given class .