ClassDiscovery::instantiateClass( string|Closure $class ): object

In this article

Get an instance of the $class.

Parameters

$classstring|Closurerequired
a FQCN of a class or a closure that instantiate the class

Return

object

Source

protected static function instantiateClass($class)
{
    try {
        if (is_string($class)) {
            return new $class();
        }
        if (is_callable($class)) {
            return $class();
        }
    } catch (\Exception $e) {
        throw new ClassInstantiationFailedException('Unexpected exception when instantiating class.', 0, $e);
    }
    throw new ClassInstantiationFailedException('Could not instantiate class because parameter is neither a callable nor a string');
}

User Contributed Notes

You must log in before being able to contribute a note or feedback.