快速检查一个对象是否可以在PHP中成功实例化?
如何在不实际创建实例的情况下检查是否可以使用给定参数成功实例化对象?
实际上我只是检查(没有测试过这段代码,但应该工作正常…)所需参数的数量,忽略类型: // 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 ); } 编辑:$构造函数可以为null … 解决方法
简短的回答是,您无法确定一组参数是否允许构造函数的无错实例化.正如评论者上面提到的那样,没有办法确定是否可以使用给定的参数列表实例化类,因为如果没有实际尝试就无法知道运行时注意事项
实例. 但是,尝试从构造函数参数列表中实例化类是有价值的.这种操作最明显的用例是可配置的依赖注入容器(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 } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |