如何从 PHP 7.4+ 中的对象中删除 属性

How to remove property from object in PHP 7.4+

如果 属性 有类型,如何删除 属性。

unset() 如果 属性 没有类型则工作。

class A
{
  public string $a;
  public $b;
}

$o = new A();
unset($o->a);
unset($o->b);

// object(A)#1 (0) {
//   ["a"]=>
//   uninitialized(string)
// }

根据 RFC on typed properties:

If a typed property is unset(), then it returns to the uninitialized state. While we would love to remove support for the unsetting of properties, this functionality is currently used for lazy initialization by Doctrine, in combination with the functionality described in the following section.