tp官方于2021年01月11日更新了6.0.6版本,已经支持了php8.0

composer update topthink/framework

PHP 8在PHP类型系统中进行了一些改进,例如,引入了Union Types,mixedtype等。

经过这些更改,Reflection API的某些方法会产生错误的结果。

在PHP8中,废弃了ReflectionParameter类中的三个方法

ReflectionParameter::getClass()

ReflectionParameter::isArray()

ReflectionParameter::isCallable()

thinkphp6框架的容器默认使用了ReflectionParameter::getClass(),在PHP8环境中直接报错中断。

Deprecated: Method ReflectionParameter::getClass() is deprecated in D:\WWW\tp6\vendor\topthink\framework\src\think\Container.php on line 443

ReflectionParameter::getClass()返回参数类的名称。但是,当使用联合类型时,null如果差异类类型在单个联合中,则它将返回。

ReflectionParameter::getType()取代getClass()方法。

请注意,getClass()方法会传回实作的ReflectionClass()物件__toString()。

如果只对名称感兴趣,则可以使用$param->getType()->getName()来返回名称

ReflectionParameter::isArray()在PHP 8中已弃用,因为它仅适用于array和?array类型,而不适用于Union类型。

ReflectionParameter::getType() result可以用来模拟这种方法的结果:

$isArray = $param->getType() && $param->getType()->getName() === 'array';

ReflectionParameter::isCallable()在PHP 8中也已弃用,与相似isArray(),它仅适用于callable和?callable参数,但在Union Type中使用时无效。

ReflectionParameter::getType()result也可以用于模拟此方法的结果isCallable:

$isCallable = $param->getType() && $param->getType()->getName() === 'callable';

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐