title: "Typed property must not be accessed before initialization" error in PHP 7.4+
body: <p>I'm using PHP 7.4 and property type hints.</p>
  <p>Let's say I have a class A, with a couple of private properties. When I use \SoapClient, Doctrine ORM, or any tool that instantiates a class bypassing the constructor and getting/setting properties directly using reflection, I face an error <code>PHP Fatal error:  Uncaught Error: Typed property A::$id must not be accessed before initialization in</code>.</p>
  <pre><code>&lt;?php
  
  declare(strict_types=1);
  
  class A
  {
      private int $id;
      private string $name;
  
      public function __construct(int $id, string $name)
      {
          $this-&gt;id   = $id;
          $this-&gt;name = $name;
      }
  
      public function getId(): int
      {
          return $this-&gt;id;
      }
  
      public function getName(): string
      {
          return $this-&gt;name;
      }
  }
  
  $a = (new \ReflectionClass(A::class))-&gt;newInstanceWithoutConstructor();
  
  var_dump($a-&gt;getId()); // Fatal error: Uncaught Error: Typed property A::$id must not be accessed before initialization in ...
  </code></pre>
  <p>I can mitigate this problem by declaring properties as nullable and setting a null value by default.</p>
  <pre><code>&lt;?php
  
  declare(strict_types=1);
  
  class A
  {
      private ?int $id      = null;
      private ?string $name = null;
  
      public function __construct(?int $id, ?string $name)
      {
          $this-&gt;id   = $id;
          $this-&gt;name = $name;
      }
  
      public function getId(): ?int
      {
          return $this-&gt;id;
      }
  
      public function getName(): ?string
      {
          return $this-&gt;name;
      }
  }
  
  $a = (new \ReflectionClass(A::class))-&gt;newInstanceWithoutConstructor();
  
  var_dump($a-&gt;getId()); // NULL
  var_dump($a-&gt;getName()); // NULL
  </code></pre>
  <p>However, I don't like this workaround. The point of my class is to be domain compliant and encapsulate domain constraints within the class design. In this case, the property <code>name</code> shouldn't be nullable. Potentially I can declare the property <code>name</code> as an empty string, but it doesn't seem like a clean solution as well.</p>
  <pre><code>&lt;?php
  
  declare(strict_types=1);
  
  class A
  {
      private ?int $id     = null;
      private string $name = '';
  
      public function __construct(?int $id, string $name)
      {
          $this-&gt;id   = $id;
          $this-&gt;name = $name;
      }
  
      public function getId(): ?int
      {
          return $this-&gt;id;
      }
  
      public function getName(): string
      {
          return $this-&gt;name;
      }
  }
  
  $a = (new \ReflectionClass(A::class))-&gt;newInstanceWithoutConstructor();
  
  var_dump($a-&gt;getId()); // NULL
  var_dump($a-&gt;getName()); // ''
  
  $idProperty = new \ReflectionProperty($a, 'id');
  $idProperty-&gt;setAccessible(true);
  if (null === $idProperty-&gt;getValue($a)) {
      $idProperty-&gt;setValue($a, 1001);
  }
  
  $nameProperty = new \ReflectionProperty($a, 'name');
  $nameProperty-&gt;setAccessible(true);
  if ('' === $nameProperty-&gt;getValue($a)) {
      $nameProperty-&gt;setValue($a, 'Name');
  }
  
  var_dump($a-&gt;getId()); // 1001
  var_dump($a-&gt;getName()); // Name
  </code></pre>
  <p><strong>My question is</strong>: is there a way to keep a proper class design and avoid facing the <code>Typed property must not be accessed before initialization</code> error? If no, what is the preferred approach to tackle this problem? (e.g. define all properties as nullable nulls or string properties as an empty string, etc.)</p>