<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 中属性的类型提示?

        Type hinting for properties in PHP 7?(PHP 7 中属性的类型提示?)
          <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 中属性的类型提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <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>
                    • <legend id='OB1KI'><style id='OB1KI'><dir id='OB1KI'><q id='OB1KI'></q></dir></style></legend>

                      1. <small id='OB1KI'></small><noframes id='OB1KI'>

                            <tbody id='OB1KI'></tbody>

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