实例
<?php class Hobby { protected $name; public function __construct($name='王五') { $this->name=$name; } public function activity($subject){ return $this->name.'喜欢'.$subject.'游戏。'; } } trait Other1 { public $classmate='张三'; public function sport($name='同学') { return $this->name.'和'.$this->classmate.'是'.$name; } public function activity($subject){ return $this->name.'喜欢'.$subject.'运动项目。'; } } trait Other2 { public $classmate1='李四'; public function sport($name='校友') { return $this->name.'和'.$this->classmate1.'是'.$name; } } class Enjoy extends Hobby { use Other1, Other2 { Other1::sport insteadof Other2; Other2::sport as mysport; } public function activity($subject = '乒乓球') { return $this->name . '喜欢' . $subject . '运动项目。'; } public function activity1() { return parent::activity($subject='射击'); } } $enjoy=new Enjoy(); //调用子类,级别高于父类 echo $enjoy->activity(); echo '<hr>'; //调用父类 echo $enjoy->activity1(); echo '<hr>'; //调用Other1 echo $enjoy->sport(); echo '<hr>'; //调用Other2 echo $enjoy->mysport(); echo '<hr>';
运行实例 »
点击 "运行实例" 按钮查看在线实例