博主信息
博文 14
粉丝 1
评论 0
访问量 29569
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
trait优先级的问题(当前类、父类、trait类)
centcool的博客
原创
1053人浏览过
<?php
/*
 * trait 优先级的问题
 * 1. 当前类中的方法与trati类中的方法重名怎么办?
 * 2. 以下实例演示:
 			当前类Demo中存在方法hello()方法
 			当前类继承的Test类中也存在hello()方法
 			trait Demo1()类中也存在 hello()方法
 			那么?优先级如何判断呢?
 * 3. 优先级:当前类Demo中的hello()方法 高于 trait Demo1类中的hello()方法 高于 当前类Demo父类Test类中的hello()方法

 */
trait Demo1{
	public function hello(){
		return __METHOD__;
	}
}
trait Demo2{
	public function hello2(){
		return __METHOD__;
	}
}
class Test{
	public function hello(){
		return __METHOD__;
	}
}
class Demo extends Test{
	// 在Demo类中继承Demo1、demo2类,使用use
	use Demo1,Demo2;
	public function hello(){
		return __METHOD__;
	}
	public function test1(){
		// 在test1方法中访问Demo1中的hello1方法
		return $this->hello1();		
	}
	public function test2(){		
		// 在test2方法中访问Demo2中的hello2方法
		return $this->hello2();
	}
}

$obj = new Demo;
echo $obj->hello(); // 输出Demo中的hello();


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学