我想在 PHP 7.4 中检查属性是否真正初始化。将属性设置为 null 意味着它是用 null 初始化的。
我无法使用,isset
因为即使它设置为 null,它也会返回 false。
我无法使用,property_exists
因为即使未初始化它也会返回 true。
我知道的唯一方法是 with 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;
Run Code Online (Sandbox Code Playgroud)
我可以使用 ReflectionProperty 编写这个函数,但也许我遗漏了一些东西?