<?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;又可以成功定义并最后成功输出?classfunctionphp