题
我可能问这个问题得厉害,所以我要举一个例子。我有一类就是类似这样的东西:
class myclass {
var $template = array();
var $record = array();
function __construct($template,$record) {
$this->template = ( set = to a database response here );
$this->record = ( set = to a database response here );
}
使用该对象时,我的问题是,模板应始终是相同的,记录是为对象的每个实例有什么变化。有没有办法有$模板进位值在每一个新的实例?类似
$a = new myclass(1,500);
$b = new myClass(2);
其中b的用于创建一个$时所已经生成$this->template
的值。也许我是从错误的角度完全接近这一点。任何建议表示赞赏。
解决方案
是。 声明它静态将使一类属性
class Counter {
public static $total = 0;
public function increment()
{
self::$total++;
}
}
echo Counter::$total; // 0;
$a = new Counter;
$a->increment();
echo $a::$total; // 1;
$b = new Counter;
echo $b::$total; // 1;
注:我用$ a和$ b访问的静态属性,以表明属性适用于这两种情况下simultaenously点。此外,这样做会从5.3工作只。在此之前,你需要做的柜台:: $总。的
不隶属于 StackOverflow