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

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

实例.

但是,尝试从构造函数参数列表中实例化类是有价值的.这种操作最明显的用例是可配置的依赖注入容器(DIC).不幸的是,这是一个比OP建议的更复杂的操作.

我们需要为提供的定义数组中的每个参数确定它是否与构造函数方法签名中指定的类型提示匹配(如果方法签名实际上具有类型提示).此外,我们需要解决如何处理默认参数值.此外,为了使我们的代码具有任何实际用途,我们需要提前规范“定义”以实例化类.对问题的复杂处理还将涉及一组反射对象(缓存),以最小化反复反射事物的性能影响.

另一个障碍是,如果不调用其ReflectionParameter :: getClass方法并随后从返回的类名中实例化反射类,则无法访问反射方法参数的类型提示(如果返回null,则param没有类型 – 暗示).这就是缓存生成的反射对于任何实际用例都变得特别重要的地方.

下面的代码是我自己的基于字符串的递归依赖注入容器的严格简化版本.它是伪代码和真实代码的混合(如果你希望免费代码复制/粘贴你运气不好).您将看到下面的代码将“definition”数组的关联数组键与构造函数签名中的参数名称相匹配.

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值