日期:2014-05-17 浏览次数:20488 次
class test1{ protected $arr = array(); function __construct($arr){ $this->arr = $arr; } function t1(){ //use $this->arr } function t2(){ //use $this->arr } }
class test2{ function get_arr(){ //get $arr from data return $arr; } function t1(){ //use $this->get_arr()) } function t2(){ //use $this->get_arr() } }
class test2{ public static $data = array(); function get_arr(){ if(self::$data) return self::$data; //get $arr from data return $arr; } function t1(){ //use $this->get_arr()) } function t2(){ //use $this->get_arr() } }
------解决方案--------------------
我认为你的两个方案都不太好,而是合并起来比较好
class test1{ protected $arr = array(); function __construct(){ $this->arr = get_arr(); } function get_arr(){ //get $arr from data return $arr; } function t1(){ //use $this->arr } function t2(){ //use $this->arr } }