簡體   English   中英

在PHP中定義公共類變量的最佳方法

[英]Best method of defining a public class variable in PHP

這些聲明和設置公共類變量的方法之間有什么區別嗎? 你有什么理由選擇其中一個嗎?

方法1

class example {

  public $myArray;

  function __construct() {
    $this->myArray = array(1, 2, 3);
  }

  function showVar() {
    print_r( $this->myArray );
  }

}

方法2

class example {

  public $myArray = array(1, 2, 3);

  function showVar() {
    print_r( $this->myArray );
  }

}

在第一種情況下,每次創建新的類實例時都會對代碼進行評估。

在第二種情況下-解析類時,它將只評估一次。

就這樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM