0 follower

Final Class Yiisoft\Hydrator\ObjectFactory\ReflectionObjectFactory

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

A factory for objects that are instantiable by using reflection.

Method Details

Hide inherited methods

create() public method

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);
}