类机制问题
上篇帖子没人气了,要问的问题没解决(估计没表达清楚)
先来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>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号