如何在不实际创建实例的情况下检查是否可以使用给定参数成功实例化对象?
实际上我只是检查(没有测试过这段代码,但应该可以正常工作......)必需的参数的数量,忽略类型:
// Filter definition and arguments as per configuration
$filter = $container->getDefinition($serviceId);
$args = $activeFilters[$filterName];
// Check number of required arguments vs arguments in config
$constructor = $reflector->getConstructor();
$numRequired = $constructor->getNumberOfRequiredParameters();
$numSpecified = is_array($args) ? count($args) : 1;
if($numRequired < $numSpecified) {
throw new InvalidFilterDefinitionException(
$serviceId,
$numRequired,
$numSpecified
);
}
编辑:$constructor
可以是null
...
答案 0 :(得分:5)
简短的回答是,您无法确定一组参数是否允许构造函数的无错误实例化。正如评论者上面提到的那样,没有办法确定是否可以使用给定的参数列表实例化类,因为如果没有实际尝试就无法知道运行时注意事项 实例
但是,尝试从构造函数参数列表中实例化一个类是有价值的。这种操作最明显的用例是可配置的依赖注入容器(DIC)。不幸的是,这比OP建议的操作复杂得多。
我们需要为提供的定义数组中的每个参数确定它是否与构造函数方法签名中指定的类型提示相匹配(如果方法签名实际上具有类型提示)。此外,我们需要解决如何处理默认参数值。此外,为了使我们的代码具有任何实际用途,我们需要提前规范“定义”以实例化类。对问题的复杂处理还将涉及一组反射对象(缓存),以最大限度地减少反复反射事物对性能的影响。
另一个障碍是,如果不调用其ReflectionParameter::getClass
方法并随后从返回的类名中实例化反射类,则无法访问反射方法参数的类型提示(如果null
返回param没有类型提示)。这就是缓存生成的反射对于任何实际用例都变得特别重要的地方。
下面的代码是我自己的基于字符串的递归依赖注入容器的严格简化版本。它是伪代码和真实代码的混合(如果你希望免费代码复制/粘贴你运气不好)。您将看到下面的代码将“definition”数组的关联数组键与构造函数签名中的参数名称相匹配。
真实代码可以在相关github project page找到。
class Provider {
private $definitions;
public function define($class, array $definition) {
$class = strtolower($class);
$this->definitions[$class] = $definition;
}
public function make($class, array $definition = null) {
$class = strtolower($class);
if (is_null($definition) && isset($this->definitions[$class])) {
$definition = $this->definitions[$class];
}
$reflClass = new ReflectionClass($class);
$instanceArgs = $this->buildNewInstanceArgs($reflClass);
return $reflClass->newInstanceArgs($instanceArgs);
}
private function buildNewInstanceArgs(
ReflectionClass $reflClass,
array $definition
) {
$instanceArgs = array();
$reflCtor = $reflClass->getConstructor();
// IF no constructor exists we're done and should just
// return a new instance of $class:
// return $this->make($reflClass->name);
// otherwise ...
$reflCtorParams = $reflCtor->getParameters();
foreach ($reflCtorParams as $ctorParam) {
if (isset($definition[$ctorParam->name])) {
$instanceArgs[] = $this->make($definition[$ctorParam->name]);
continue;
}
$typeHint = $this->getParameterTypeHint($ctorParam);
if ($typeHint && $this->isInstantiable($typeHint)) {
// The typehint is instantiable, go ahead and make a new
// instance of it
$instanceArgs[] = $this->make($typeHint);
} elseif ($typeHint) {
// The typehint is abstract or an interface. We can't
// proceed because we already know we don't have a
// definition telling us which class to instantiate
throw Exception;
} elseif ($ctorParam->isDefaultValueAvailable()) {
// No typehint, try to use the default parameter value
$instanceArgs[] = $ctorParam->getDefaultValue();
} else {
// If all else fails, try passing in a NULL or something
$instanceArgs[] = NULL;
}
}
return $instanceArgs;
}
private function getParameterTypeHint(ReflectionParameter $param) {
// ... see the note about retrieving parameter typehints
// in the exposition ...
}
private function isInstantiable($class) {
// determine if the class typehint is abstract/interface
// RTM on reflection for how to do this
}
}