日期:2014-05-17  浏览次数:20534 次

请问有没有人用过traits类型?有个问题需要问

<?php
trait Counter {
static $a = 5;
public function inc() {
static $c = 0;
$c += 1;
echo "$c<br />";
}
}

class C1 {
use Counter;
}

class C2 {
use Counter;
}



$o = new C1();
$o->inc();
echo Counter::$a;
echo '<br />';

$p = new C2();
$p->inc();
?>


手册上讲“静态变量可以被 trait 的方法引用,但不能被 trait 定义。但是 traits 能够为使用的类定义静态方法”,但为什么我这个例子中的static $a = 5;又可以成功定义并最后成功输出?
class function php

------解决方案--------------------
trait 是 php 5.4 才加入的
你的手册是最新的吗?