Final Class Yiisoft\Hydrator\ObjectFactory\ReflectionObjectFactory
| Inheritance | Yiisoft\Hydrator\ObjectFactory\ReflectionObjectFactory |
|---|---|
| Implements | Yiisoft\Hydrator\ObjectFactory\ObjectFactoryInterface |
A factory for objects that are instantiable by using reflection.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| create() | Yiisoft\Hydrator\ObjectFactory\ReflectionObjectFactory |
Method Details
| 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);
}
Signup or Login in order to comment.