• <i id='VF2Fu'><tr id='VF2Fu'><dt id='VF2Fu'><q id='VF2Fu'><span id='VF2Fu'><b id='VF2Fu'><form id='VF2Fu'><ins id='VF2Fu'></ins><ul id='VF2Fu'></ul><sub id='VF2Fu'></sub></form><legend id='VF2Fu'></legend><bdo id='VF2Fu'><pre id='VF2Fu'><center id='VF2Fu'></center></pre></bdo></b><th id='VF2Fu'></th></span></q></dt></tr></i><div id='VF2Fu'><tfoot id='VF2Fu'></tfoot><dl id='VF2Fu'><fieldset id='VF2Fu'></fieldset></dl></div>
    <tfoot id='VF2Fu'></tfoot>

    <small id='VF2Fu'></small><noframes id='VF2Fu'>

    1. <legend id='VF2Fu'><style id='VF2Fu'><dir id='VF2Fu'><q id='VF2Fu'></q></dir></style></legend>
        <bdo id='VF2Fu'></bdo><ul id='VF2Fu'></ul>

        为什么我突然收到“在初始化之前不能访问类型化属性"?引入属性类型提示时出错?

        时间:2023-08-19

          <small id='jSlAJ'></small><noframes id='jSlAJ'>

            <bdo id='jSlAJ'></bdo><ul id='jSlAJ'></ul>
            <tfoot id='jSlAJ'></tfoot>

                  <tbody id='jSlAJ'></tbody>

              • <legend id='jSlAJ'><style id='jSlAJ'><dir id='jSlAJ'><q id='jSlAJ'></q></dir></style></legend>
              • <i id='jSlAJ'><tr id='jSlAJ'><dt id='jSlAJ'><q id='jSlAJ'><span id='jSlAJ'><b id='jSlAJ'><form id='jSlAJ'><ins id='jSlAJ'></ins><ul id='jSlAJ'></ul><sub id='jSlAJ'></sub></form><legend id='jSlAJ'></legend><bdo id='jSlAJ'><pre id='jSlAJ'><center id='jSlAJ'></center></pre></bdo></b><th id='jSlAJ'></th></span></q></dt></tr></i><div id='jSlAJ'><tfoot id='jSlAJ'></tfoot><dl id='jSlAJ'><fieldset id='jSlAJ'></fieldset></dl></div>

                  本文介绍了为什么我突然收到“在初始化之前不能访问类型化属性"?引入属性类型提示时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经更新了我的类定义以使用新引入的属性类型提示,如下所示:

                  I have updated my class definitions to make use of the newly introduced property type hints, like this:

                  class Foo {
                  
                      private int $id;
                      private ?string $val;
                      private DateTimeInterface $createdAt;
                      private ?DateTimeInterface $updatedAt;
                  
                      public function __construct(int $id) {
                          $this->id = $id;
                      }
                  
                  
                      public function getId(): int { return $this->id; }
                      public function getVal(): ?string { return $this->val; }
                      public function getCreatedAt(): ?DateTimeInterface { return $this->createdAt; }
                      public function getUpdatedAt(): ?DateTimeInterface { return $this->updatedAt; }
                  
                      public function setVal(?string $val) { $this->val = $val; }
                      public function setCreatedAt(DateTimeInterface $date) { $this->createdAt = $date; }
                      public function setUpdatedAt(DateTimeInterface $date) { $this->updatedAt = $date; }
                  }
                  

                  但是当我尝试在 Doctrine 上保存我的实体时,我收到一条错误消息:

                  But when trying to save my entity on Doctrine I am getting an error saying:

                  在初始化之前不能访问类型化的属性

                  Typed property must not be accessed before initialization

                  这不仅发生在 $id$createdAt,而且发生在 $value$updatedAt>,它们是可为空的属性.

                  This not only happens with $id or $createdAt, but also happen with $value or $updatedAt, which are nullable properties.

                  推荐答案

                  由于 PHP 7.4 为属性引入了类型提示,因此为所有属性提供有效值尤为重要,以便所有属性都具有与其声明类型匹配的值.

                  Since PHP 7.4 introduces type-hinting for properties, it is particularly important to provide valid values for all properties, so that all properties have values that match their declared types.

                  从未赋值的属性没有 null 值,但它处于 undefined 状态,永远不会匹配任何声明的类型.undefined !== null.

                  A property that has never been assigned doesn't have a null value, but it is on an undefined state, which will never match any declared type. undefined !== null.

                  对于上面的代码,如果你这样做了:

                  For the code above, if you did:

                  $f = new Foo(1);
                  $f->getVal();
                  

                  你会得到:

                  致命错误:未捕获错误:类型化属性 Foo::$val 不能在初始化前访问

                  Fatal error: Uncaught Error: Typed property Foo::$val must not be accessed before initialization

                  因为$val在访问时既不是string也不是null.

                  Since $val is neither string nor nullwhen accessing it.

                  解决这个问题的方法是为所有与声明类型匹配的属性赋值.您可以将此作为属性的默认值或在构建期间执行此操作,具体取决于您的偏好和属性类型.

                  The way to get around this is to assign values to all your properties that match the declared types. You can do this either as default values for the property or during construction, depending on your preference and the type of the property.

                  例如,对于上面的一个可以这样做:

                  For example, for the above one could do:

                  class Foo {
                  
                      private int $id;
                      private ?string $val = null; // <-- declaring default null value for the property
                      private Collection $collection;
                      private DateTimeInterface $createdAt;
                      private ?DateTimeInterface $updatedAt;
                  
                      public function __construct(int $id) {
                          // and on the constructor we set the default values for all the other 
                          // properties, so now the instance is on a valid state
                          $this->id = $id;
                          $this->createdAt = new DateTimeImmutable();
                          $this->updatedAt = new DateTimeImmutable();
                  
                          $this->collection = new ArrayCollection();
                      }
                  

                  现在所有属性都将具有 valid 值,并且实例将处于有效状态.

                  Now all properties would have a valid value and the the instance would be on a valid state.

                  当您依赖来自数据库的值作为实体值时,这种情况尤其常见.例如.自动生成的 ID,或创建和/或更新的值;这通常是数据库关注的问题.

                  This can hit particularly often when you are relying on values that come from the DB for entity values. E.g. auto-generated IDs, or creation and/or updated values; which often are left as a DB concern.

                  对于自动生成的 ID,推荐的前进方式是将类型声明更改为:

                  For auto-generated IDs, the recommended way forward is to change the type declaration to:

                  private ?int $id = null
                  

                  对于其余的,只需为属性的类型选择一个合适的值即可.

                  For all the rest, just choose an appropriate value for the property's type.

                  这篇关于为什么我突然收到“在初始化之前不能访问类型化属性"?引入属性类型提示时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:递归 chmod/chown/chgrp 目录中的所有文件和文件夹 下一篇:如何将 WHERE IN 与 Doctrine 2 一起使用

                  相关文章

                    <bdo id='4NrIM'></bdo><ul id='4NrIM'></ul>
                • <i id='4NrIM'><tr id='4NrIM'><dt id='4NrIM'><q id='4NrIM'><span id='4NrIM'><b id='4NrIM'><form id='4NrIM'><ins id='4NrIM'></ins><ul id='4NrIM'></ul><sub id='4NrIM'></sub></form><legend id='4NrIM'></legend><bdo id='4NrIM'><pre id='4NrIM'><center id='4NrIM'></center></pre></bdo></b><th id='4NrIM'></th></span></q></dt></tr></i><div id='4NrIM'><tfoot id='4NrIM'></tfoot><dl id='4NrIM'><fieldset id='4NrIM'></fieldset></dl></div>
                • <small id='4NrIM'></small><noframes id='4NrIM'>

                  <tfoot id='4NrIM'></tfoot>
                  <legend id='4NrIM'><style id='4NrIM'><dir id='4NrIM'><q id='4NrIM'></q></dir></style></legend>