我想在 PHP 7.4 中检查属性是否真正初始化。将属性设置为 null 意味着它是用 null 初始化的。
我不能使用isset
因为即使设置为 null,它也会返回 false。
我不能使用property_exists
因为即使未初始化它也会返回 true。
我知道的唯一方法就是ReflectionProperty::isInitialized
,但这感觉有点奇怪,我需要走这条弯路。
class DTO {
public ?string $something;
}
$object = new DTO();
$object->something = null;
isset($object->something); //returns false
is_initialized($object->something); //should return true;
我可以使用 ReflectionProperty 编写这个函数,但也许我遗漏了一些东西?
不鼓励这样做,请参阅下面的对话此拒绝实施拉动请求is_initialized原生 PHP 函数 https://github.com/php/php-src/pull/7029.
但如果确实需要,可以使用反射属性::已初始化 https://www.php.net/manual/en/reflectionproperty.isinitialized.php method.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)