1 class Person
2 {
3 public $name;
4 static public $num;
5
6 public function __construct($n){
7 $this->name = $n;
8 Person::$num++;
9 }
10 }
11 new Person('user1');
12 new Person('user2');
13 new Person('user3');
14
15 echo Person::$num; # 3
1 # 类名可以直接调用不包含$this的方法,否则必须通过new类名调用
2
3 class Person
4 {
5 public $name;
6
7 public function __construct($n){
8 $this->name = $n;
9 }
10
11 public function say(){
12 echo "<p>my name is {$this->name}</p>";
13 }
14
15 static public function sum($i,$j){
16 return $i+$j;
17 }
18 }
19
20 echo Person::sum(5,25);