as可以修改traits里面方法的可见性
public function aa(){ echo "tA:aa...
"; } public function bb(){ echo "tA:bb...
"; } public function ok(){ echo "tA:ok...
"; } } trait tB { public function cc(){ echo "tB:cc...
"; } public function dd(){ echo "tB:dd...
"; } public function ok(){ echo "tB:ok...
"; } } //抽象类 abstract class Humanity { public $name; public $sex; public $iq=10; const BIRTHPLACE='地球'; static public $counter=0;//静态属性,它是公共的资源和具体的实例没有关系 //构造函数 public function __construct($name,$sex){ self::$counter++; $this->name=$name; $this->sex=$sex; } protected function chew($food){ echo "{$food}已经被咀嚼完成!
"; } //抽象方法 abstract public function eat($food); static public function hello(){ echo '您好!我是来自'.self::BIRTHPLACE.'的人类
'; } } class Student extends Humanity { const BIRTHPLACE='火星'; public $studentId; public function test($subject){ echo "{$this->name}正在考{$subject}!
"; } public function eat($food){ $this->chew($food); echo "{$this->name}正在快速的吃{$food}!
"; } use tA,tB { tA::OK insteadof tB; tB::ok as okTb; aa as private; bb as private bbPrivate; } } abstract class Dog { } class Huskies extends Dog { use tA,tB { tB::ok insteadof tA; } } $hanMM=new Student('韩梅梅','女'); $hanMM->bbPrivate();
解析:
tA语法糖里面的ok方法替换掉tB里面的ok方法。
tB语法糖里面的ok方法改名为okTb。
aa方法把修饰符替换成私有性private。
把bb方法改名为bbPrivate,并把修饰符改成private.
public function aa(){ echo "tA:aa...
"; } public function bb(){ echo "tA:bb...
"; } public function ok(){ echo "tA:ok...
"; } } trait tB { public function cc(){ echo "tB:cc...
"; } public function dd(){ echo "tB:dd...
"; } public function ok(){ echo "tB:ok...
"; } } trait tC { use tA,tB { tA::ok insteadof tB; } } //抽象类 abstract class Humanity { public $name; public $sex; public $iq=10; const BIRTHPLACE='地球'; static public $counter=0;//静态属性,它是公共的资源和具体的实例没有关系 //构造函数 public function __construct($name,$sex){ self::$counter++; $this->name=$name; $this->sex=$sex; } protected function chew($food){ echo "{$food}已经被咀嚼完成!
"; } //抽象方法 abstract public function eat($food); static public function hello(){ echo '您好!我是来自'.self::BIRTHPLACE.'的人类
'; } } class Student extends Humanity { const BIRTHPLACE='火星'; public $studentId; public function test($subject){ echo "{$this->name}正在考{$subject}!
"; } public function eat($food){ $this->chew($food); echo "{$this->name}正在快速的吃{$food}!
"; } use tC; } abstract class Dog { } class Huskies extends Dog { use tC; } $hanMM=new Student('韩梅梅','女'); $hanMM->cc();
解析:
tC里面有两个语法糖(有六个方法),先替换掉冲突的ok不然会报错,然后输出即可(算是tC有六个方法).
public function aa(){ echo "tA:aa...
"; } public function bb(){ echo "tA:bb...
"; } abstract public function ok(); } //抽象类 abstract class Humanity { public $name; public $sex; public $iq=10; const BIRTHPLACE='地球'; static public $counter=0;//静态属性,它是公共的资源和具体的实例没有关系 //构造函数 public function __construct($name,$sex){ self::$counter++; $this->name=$name; $this->sex=$sex; } protected function chew($food){ echo "{$food}已经被咀嚼完成!
"; } //抽象方法 abstract public function eat($food); static public function hello(){ echo '您好!我是来自'.self::BIRTHPLACE.'的人类
'; } } class Student extends Humanity { const BIRTHPLACE='火星'; public $studentId; public function test($subject){ echo "{$this->name}正在考{$subject}!
"; } public function eat($food){ $this->chew($food); echo "{$this->name}正在快速的吃{$food}!
"; } use tA; public function ok(){ } } abstract class Dog { } class Huskies extends Dog { use tA; public function ok(){ } } $hanMM=new Student('韩梅梅','女'); $hanMM->aa();
解析: 重写ok方法,不然会报错。