<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                本篇主要說下PHP8構造器屬性提升的用法,這個特性對于一些需要在構造器中設置或初始化一些類屬性的時候非常有用(包括`public`、`protected`和`private`),比如在PHP7中你可以這樣定義一個類的屬性,然后在構造方法中傳值。 ~~~php class Point { public int $x; private string $y; public function __construct(int $x = 0, string $y='') { $this->x = $x; $this->y = $y; } } ~~~ 在PHP8中則可以簡化為直接在構造函數方法里面定義類的屬性 ~~~php class Point { public function __construct(public int $x = 0, private string $y = '') { // 你可以在構造器中直接輸出類的x和y屬性的值(也就是傳入的x和y變量的值) var_dump($this->x); var_dump($this->y); } } ~~~ 不過需要注意的是構造器屬性只能在構造器方法中定義(好像是廢話),而且必須添加`public`/`protected`/`private`,如果不添加的話是不會作為類屬性定義的,除非你在父類中已經定義了構造器屬性,比如: ~~~php class Test { public function __construct( public int $x = 0 ) {} } class Child extends Test { public function __construct( $x, public int $y = 0, public int $z = 0, ) { parent::__construct($x); } } ~~~ 在實際使用過程中,還有些細節需要注意。 ### 抽象類或接口的構造器方法不能使用構造器屬性定義 ~~~php abstract class Test { // 錯誤 abstract public function __construct(private $x); } interface Test { // 錯誤 public function __construct(private $x); } ~~~ 但Traits中是允許使用的 ``` trait MyTrait { public function __construct( public string $a, ) {} } ``` ### 對象類型的構造器屬性不能使用`null`作為默認值 如果你的構造器屬性的類型是一個對象,那么不可以使用`null`作為參數默認值 ~~~php class Test { // 錯誤 public function __construct(public Type $prop = null) {} } ~~~ 但可以使用 ~~~php class Test { // 正確 public function __construct(public ?Type $prop = null) {} } ~~~ ### 不支持`callable`類型的構造器屬性定義 ~~~php class Test { // 錯誤 public function __construct(public callable $callback) {} } ~~~ ### 構造器屬性不能使用`var`定義 ~~~php class Test { // 錯誤 public function __construct(var $prop) {} } ~~~ ### 構造器屬性定義也不能使用可變參數 ~~~php class Test { // Error: Variadic parameter. public function __construct(public string ...$strings) {} } ~~~ ### 構造器屬性和類屬性不能重復定義 比如 ~~~php class Test { public string $prop; public int $explicitProp; // Error: Redeclaration of property. public function __construct(public string $prop) {} } ~~~ 但你可以使用構造器屬性定義額外的尚未定義過的類屬性 ~~~php class Test { public string $prop; public int $explicitProp; // Correct public function __construct(public int $promotedProp, int $arg) { $this->explicitProp = $arg; } } ~~~ ### 只能使用簡單默認值 比如,你不可以在參數的默認值中使用函數或者實例化對象。 ``` public function __construct( public string $name = 'Brent', // 錯誤 public DateTime $date = new DateTime(), ) {} ``` 更多的用法可以自行研究。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看