0

Final Class Yiisoft\Hydrator\ObjectFactory\ReflectionObjectFactory

InheritanceYiisoft\Hydrator\ObjectFactory\ReflectionObjectFactory
ImplementsYiisoft\Hydrator\ObjectFactory\ObjectFactoryInterface

A factory for objects that are instantiable by using reflection.

Public Methods

Hide inherited methods

Method Description Defined By
create() Creates an object of the given class. Yiisoft\Hydrator\ObjectFactory\ReflectionObjectFactory

Method Details

Hide inherited methods

create() public method

Creates an object of the given class.

Callers must not pass enums to this method, so implementations aren't required to handle them.

public object create ( ReflectionClass $reflectionClass, array $constructorArguments )
$reflectionClass ReflectionClass
$constructorArguments array
throws Yiisoft\Hydrator\Exception\AbstractClassException
throws Yiisoft\Hydrator\Exception\NonPublicConstructorException
throws Yiisoft\Hydrator\Exception\WrongConstructorArgumentsCountException

                public function create(ReflectionClass $reflectionClass, array $constructorArguments): object
{
    if ($reflectionClass->isAbstract()) {
        throw new AbstractClassException($reflectionClass);
    }
    $constructor = $reflectionClass->getConstructor();
    if ($constructor !== null) {
        if (!$constructor->isPublic()) {
            throw new NonPublicConstructorException($constructor);
        }
        $countArguments = count($constructorArguments);
        if ($constructor->getNumberOfRequiredParameters() > $countArguments) {
            throw new WrongConstructorArgumentsCountException($constructor, $countArguments);
        }
    }
    return $reflectionClass->newInstance(...$constructorArguments);
}