繁体   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