[英]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.