public function get($serviceName)
    {
        if (! $this->has($serviceName)) {
            throw new NotFoundException('Error: Could not find service');
        }

        $serviceProvider = $this->serviceProviders[$serviceName];

        if (! is_string($serviceProvider)) {
            throw new ContainerException(
                'Error: Expected class name of service provider as string but got "'.gettype($serviceProvider).'"'
            );
        }

        try {
            // The name of the service provider class is stored, try to create an object from it
            /** @var AbstractServiceProvider $serviceProvider */
            $serviceProvider = new $serviceProvider($serviceName, $this);
        } catch (\Exception $exception) {
            throw new ContainerException('Error: Could not create service provider via reflection');
        }

        if (! is_a($serviceProvider, AbstractServiceProvider::class)) {
            throw new ContainerException(
                'Error: Service provider object does not inherit from AbstractServiceProvider'
            );
        }

        try {
            // Try to make the provider provide the service. Might throw an exception.
            $object = $serviceProvider->provide();
        } catch (\Exception $exception) {
            if (is_a($exception, ContainerException::class)) {
                /** @var ContainerException $exception */
                throw $exception;
            } else {
                throw new ContainerException(
                    'Error: Service provider could not create service. Reason: '.$exception->getMessage()
                );
            }
        }

        if (! is_object($object)) {
            throw new ContainerException('Error: Service provider did not provide a valid service object');
        }

        return $object;
    }
public function get($serviceName)
    {
        if (! $this->has($serviceName)) {
            throw new NotFoundException('Error: Could not find service');
        }

        $serviceProvider = $this->serviceProviders[$serviceName];

        if (! is_string($serviceProvider)) {
            throw new ContainerException(
                'Error: Expected class name of service provider as string but got "'.gettype($serviceProvider).'"'
            );
        }

        try {
            // The name of the service provider class is stored, try to create an object from it
            /** @var AbstractServiceProvider $serviceProvider */
            $serviceProvider = new $serviceProvider($serviceName, $this);
        } catch (\Exception $exception) {
            throw new ContainerException('Error: Could not create service provider via reflection');
        }

        if (! is_a($serviceProvider, AbstractServiceProvider::class)) {
            throw new ContainerException(
                'Error: Service provider object does not inherit from AbstractServiceProvider'
            );
        }

        try {
            // Try to make the provider provide the service. Might throw an exception.
            $object = $serviceProvider->provide();
        } catch (\Exception $exception) {
            if (is_a($exception, ContainerException::class)) {
                /** @var ContainerException $exception */
                throw $exception;
            } else {
                throw new ContainerException(
                    'Error: Service provider could not create service. Reason: '.$exception->getMessage()
                );
            }
        }

        if (! is_object($object)) {
            throw new ContainerException('Error: Service provider did not provide a valid service object');
        }

        return $object;
    }
public function get ( $ serviceName ) { if ( ! $ this -> has ( $ serviceName ) ) { throw new NotFoundException ( 'Error: Could not find service' ) ; } $ serviceProvider = $ this -> serviceProviders [ $ serviceName ] ; if ( ! is_string ( $ serviceProvider ) ) { throw new ContainerException ( 'Error: Expected class name of service provider as string but got "' . gettype ( $ serviceProvider ) . '"' ) ; } try { // The name of the service provider class is stored, try to create an object from it /** @var AbstractServiceProvider $serviceProvider */ $ serviceProvider = new $ serviceProvider ( $ serviceName , $ this ) ; } catch ( \ Exception $ exception ) { throw new ContainerException ( 'Error: Could not create service provider via reflection' ) ; } if ( ! is_a ( $ serviceProvider , AbstractServiceProvider :: class ) ) { throw new ContainerException ( 'Error: Service provider object does not inherit from AbstractServiceProvider' ) ; } try { // Try to make the provider provide the service. Might throw an exception. $ object = $ serviceProvider -> provide ( ) ; } catch ( \ Exception $ exception ) { if ( is_a ( $ exception , ContainerException :: class ) ) { /** @var ContainerException $exception */ throw $ exception ; } else { throw new ContainerException ( 'Error: Service provider could not create service. Reason: ' . $ exception -> getMessage ( ) ) ; } } if ( ! is_object ( $ object ) ) { throw new ContainerException ( 'Error: Service provider did not provide a valid service object' ) ; } return $ object ; }
Returns a service. The service name has to be registered.

@param string $serviceName
@return object
@throws ContainerException
@throws NotFoundException
Returns a service . The service name has to be registered .