Home >Backend Development >PHP Tutorial > 类机制有关问题
类机制问题
上篇帖子没人气了,要问的问题没解决(估计没表达清楚)
先来2个类,这2个类主要是源数据的来源途径不同
Test1类
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> class test1{ protected $arr = array(); function __construct($arr){ $this->arr = $arr; } function t1(){ //use $this->arr } function t2(){ //use $this->arr } }
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> 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() } } <br><font color="#e78608">------解决方案--------------------</font><br> 我认为你的两个方案都不太好,而是合并起来比较好
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 } } <div class="clear"> </div>