<?php
trait Test1{
public $name='php中文网';
public function hello1(){
return 'Test1::hello()';
}
}
trait Test2{
use Test1;
public function hello2(){
return 'Test2::Hello()'.$this->name;
}
}
class Demo{
public function hello2(){
return '父类Demo::Hello2()';
}
}
class Demo1 extends Demo{
//同一类中,同名访问优先级:子类>trait类>父类
use Test2;
public function hello2(){
return '子类Demo1::Hello2()';
}
}
$obj=new Demo1();
echo $obj->hello1();
echo '<hr>';
echo $obj->name;
echo '<hr>';
echo $obj->hello2();
echo '<hr>';
?>