快速检查一个对象是否可以在PHP中成功实例化?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了快速检查一个对象是否可以在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 …

解决方法

@H_360_16@ 简短的回答是,您无法确定一组参数是否允许构造函数的无错实例化.正如评论者上面提到的那样,没有办法确定是否可以使用给定的参数列表实例化类,因为如果没有实际尝试就无法知道运行时注意事项
实例.

但是,尝试从构造函数参数列表中实例化类是有价值的.这种操作最明显的用例是可配置的依赖注入容器(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 &amp;& $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
    }
}

脚本宝典总结

以上是脚本宝典为你收集整理的快速检查一个对象是否可以在PHP中成功实例化?全部内容,希望文章能够帮你解决快速检查一个对象是否可以在PHP中成功实例化?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。