<tfoot id='9AjiK'></tfoot>

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

      1. <small id='9AjiK'></small><noframes id='9AjiK'>

        <legend id='9AjiK'><style id='9AjiK'><dir id='9AjiK'><q id='9AjiK'></q></dir></style></legend>

        PHP 7 中属性的类型提示?

        时间:2024-04-13
          <tbody id='SMKac'></tbody>

      2. <legend id='SMKac'><style id='SMKac'><dir id='SMKac'><q id='SMKac'></q></dir></style></legend>
            • <bdo id='SMKac'></bdo><ul id='SMKac'></ul>

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

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

                  本文介绍了PHP 7 中属性的类型提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  php 7 是否支持类属性的类型提示?

                  Does php 7 support type hinting for class properties?

                  我的意思是,不仅针对 setters/getters,还针对属性本身.

                  I mean, not just for setters/getters but for the property itself.

                  类似:

                  class Foo {
                      /**
                       *
                       * @var Bar
                       */
                      public $bar : Bar;
                  }
                  
                  $fooInstance = new Foo();
                  $fooInstance->bar = new NotBar(); //Error
                  

                  推荐答案

                  PHP 7.4 将支持类型化属性 像这样:

                  PHP 7.4 will support typed properties like so:

                  class Person
                  {
                      public string $name;
                      public DateTimeImmutable $dateOfBirth;
                  }
                  

                  <小时>

                  PHP 7.3 及更早版本不支持此功能,但有一些替代方案.


                  PHP 7.3 and earlier do not support this, but there are some alternatives.

                  您可以创建一个只能通过具有类型声明的 getter 和 setter 访问的私有属性:

                  You can make a private property which is accessible only through getters and setters which have type declarations:

                  class Person
                  {
                      private $name;
                      public function getName(): string {
                          return $this->name;
                      }
                      public function setName(string $newName) {
                          $this->name = $newName;
                      }
                  }
                  

                  您还可以创建公共属性并使用 docblock 向阅读代码和使用 IDE 的人提供类型信息,但这不提供运行时类型检查:

                  You can also make a public property and use a docblock to provide type information to people reading the code and using an IDE, but this provides no runtime type-checking:

                  class Person
                  {
                      /**
                        * @var string
                        */
                      public $name;
                  }
                  

                  确实,您可以将 getter 和 setter 以及 docblock 结合起来.

                  And indeed, you can combine getters and setters and a docblock.

                  如果你更喜欢冒险,你可以用 __get__set__isset__unset 魔术方法,自己检查类型.不过,我不确定我是否会推荐它.

                  If you're more adventurous, you could make a fake property with the __get, __set, __isset and __unset magic methods, and check the types yourself. I'm not sure if I'd recommend it, though.

                  这篇关于PHP 7 中属性的类型提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我的 Magento 扩展安装脚本将无法运行 下一篇:使用 PHP DOMDocument 更改标签属性值

                  相关文章

                    <bdo id='OB1KI'></bdo><ul id='OB1KI'></ul>

                  <i id='OB1KI'><tr id='OB1KI'><dt id='OB1KI'><q id='OB1KI'><span id='OB1KI'><b id='OB1KI'><form id='OB1KI'><ins id='OB1KI'></ins><ul id='OB1KI'></ul><sub id='OB1KI'></sub></form><legend id='OB1KI'></legend><bdo id='OB1KI'><pre id='OB1KI'><center id='OB1KI'></center></pre></bdo></b><th id='OB1KI'></th></span></q></dt></tr></i><div id='OB1KI'><tfoot id='OB1KI'></tfoot><dl id='OB1KI'><fieldset id='OB1KI'></fieldset></dl></div>
                2. <legend id='OB1KI'><style id='OB1KI'><dir id='OB1KI'><q id='OB1KI'></q></dir></style></legend>
                  1. <small id='OB1KI'></small><noframes id='OB1KI'>

                      <tfoot id='OB1KI'></tfoot>