我正在使用 PHP 7.4 和属性类型提示。
假设我有一个类 A,它有几个私有属性。当我使用 \SoapClient、Doctrine ORM 或任何绕过构造函数直接使用反射获取/设置属性的工具来实例化一个类时,我会遇到错误 PHP Fatal error: Uncaught Error: Typed property A::$id must not be accessed before initialization in
。
<?php
declare(strict_types=1);
class A
{
private int $id;
private string $name;
public function __construct(int $id, string $name)
{
$this->id = $id;
$this->name = $name;
}
public function getId(): int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
}
$a = (new \ReflectionClass(A::class))->newInstanceWithoutConstructor();
var_dump($a->getId()); // Fatal error: Uncaught Error: Typed property A::$id must not be accessed before initialization in ...
我可以通过将属性声明为可空并默认设置为空值来减轻此问题。
<?php
declare(strict_types=1);
class A
{
private ?int $id = null;
private ?string $name = null;
public function __construct(?int $id, ?string $name)
{
$this->id = $id;
$this->name = $name;
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
}
$a = (new \ReflectionClass(A::class))->newInstanceWithoutConstructor();
var_dump($a->getId()); // NULL
var_dump($a->getName()); // NULL
然而,我不喜欢这种解决方案。我的类的目的是遵守领域规范并将领域限制封装在类设计中。在这种情况下,属性
name
不应该为空。潜在地,我可以将属性name
声明为空字符串,但这似乎也不是一个干净的解决方案。<?php
declare(strict_types=1);
class A
{
private ?int $id = null;
private string $name = '';
public function __construct(?int $id, string $name)
{
$this->id = $id;
$this->name = $name;
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
}
$a = (new \ReflectionClass(A::class))->newInstanceWithoutConstructor();
var_dump($a->getId()); // NULL
var_dump($a->getName()); // ''
$idProperty = new \ReflectionProperty($a, 'id');
$idProperty->setAccessible(true);
if (null === $idProperty->getValue($a)) {
$idProperty->setValue($a, 1001);
}
$nameProperty = new \ReflectionProperty($a, 'name');
$nameProperty->setAccessible(true);
if ('' === $nameProperty->getValue($a)) {
$nameProperty->setValue($a, 'Name');
}
var_dump($a->getId()); // 1001
var_dump($a->getName()); // Name
我的问题是:有没有一种方法可以保持良好的类设计并避免遇到“Typed property must not be accessed before initialization”错误?如果没有,解决这个问题的首选方法是什么?(例如将所有属性定义为可空null或将字符串属性定义为空字符串等)
private int $id = 0;
。如果不这样做,初始值将为NULL。 - Markus ZellergetId(): int
时,您期望得到一个整数,但当它为 NULL 时,显然是不正确的。因此,您需要返回一个整数,并像这样检查:return (int)$this->id;
。然后,NULL 将被转换为 0。或者将签名更改为getId(): ?int
。 - Markus Zeller