时间:2021-07-01 10:21:17 帮助过:22人阅读
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 } }