我想在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编写此函数,但也许我错过了什么?
ReflectionProperty::isInitialized
是解决这个问题的方法。 - Heiko Jerichen