php类作用域和trait

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

trait Common{
function (){
echo $this->name." Common Startn";
}
}

class Car{
function __construct(){
echo "Car Initn";
}
public function drive(){
echo "Car Dirven";
}

public function (){
echo "Car startn";
}
}

class Bus extends Car{
use Common;

private static $type = 'BMW';
private $name = 'Bus';

function __construct(){
parent::__construct();
echo "Bus Initn";
}
public function drive(){
parent::drive();
echo "Bus Dirven";
$this::start();
$this->start();
self::start();

$this::turn();
$this->turn();
self::turn();
// self->turn();
}
// public function start(){
// echo $this->name." Startn";
// }
private function stop(){
echo $this->name." stopn";
}
private static function turn(){
echo "Car turnn";
}
}

$bus = new Bus();
$bus->drive();

运行结果

1
2
3
4
5
6
7
8
9
10
Car Init
Bus Init
Car Dirve
Bus Dirve
Bus Common Start
Bus Common Start
Bus Common Start
Car turn
Car turn
Car turn