您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

一文聊聊php5.4的新功能Traits

2024/3/15 14:27:40发布28次查看
traits是在5.4中新增的一个用于实现代码重用的方法。
php是一种单一继承的语言,我们无法像java一样在一个class中extends多个基类来实现代码重用,现在traits能解决这一代码重用的问题,它能让开发者在多个不同的class中实现代码重用。
traits和class在语义的定义上都是为了减少代码的复杂性,避免多重继承的问题。
traits 和class相似,但是仅用于以统一和较细粒度的方式来提供一组功能,在traits内部无法进行实例化,即不存在类似class的构造函数__construct()。traits作为一个php传统继承的扩展并实现水平集成;因此,在应用程序的class中可以不再需要继承。
1)如何使用
在类中用关键字'use' 来引用 traits。多个traits 用','隔开。
实例代码如下:
e1b5a59d59df21b94de83e7d48c88142
2)优先级
基类中的成员函数将被traits中的函数覆盖,当前类中的成员函数将覆盖traits中的函数。
15347c240225d20be6c2913eb1e79b0esayhello();$oe = new myhelloworldext();$oe->sayhello();echo \n;$oe->shortarray();
输出:
hello world!afirst
3)多traits
多个traits可以添加到一个class的声明中,多个traits之间用","隔开。
<?phptrait hello { public function sayhello() { echo 'hello '; }}trait world { public function sayworld() { echo 'world'; }}class myhelloworld { use hello, world;}$o = new myhelloworld();$o->sayhello();$o->sayworld();?>
输出结果:
hello world
4)多traits冲突
如果添加到同一个class的两个traits中有相同的函数名称,且没有明确的进行处理,将产生一个错误。 为了解决同一个类中两个tratis中的同名函数冲突,需要用insteadof操作符来选择正确的函数。 因为方法的唯一性和排他性,'as'操作符允许用在冲突函数之后以解决内部冲突的问题。
<?phptrait a {public function smalltalk() {echo 'a';}public function bigtalk() {echo 'a';}}trait b {public function smalltalk() {echo 'b';}public function bigtalk() {echo 'b';}}class talker {use a, b {b::smalltalk insteadof a;a::bigtalk insteadof b;}}class aliased_talker {use a, b {b::smalltalk insteadof a;a::bigtalk insteadof b;b::bigtalk as talk;}}?>
上面的例子中,talker使用traits a 和b,因此两者中相同的函数名称存在冲突。
alker中定义了smalltalk取自traits b,bigtalk取自traits a。 aliased_talker中通过使用as操作符来确保traits b中的bigtalk通过别名talk来实现。
5)改变函数访问权限
我们可以使用as语法来改变traits中函数的访问权限属性。
<?phptrait helloworld {public function sayhello() {echo 'hello world!';}}// change visibility of sayhello,改变sayhello的访问权限。class myclass1 {use helloworld { sayhello as protected; }}// alias method with changed visibility// sayhello visibility not changed,设置别名myprivatehello。class myclass2 {use helloworld { sayhello as private myprivatehello; }}?>
6)traits组成新traits
就像许多类一样可以在类中使用traits,traits中一样可以使用traits。可以在一个traits中定义一个或者多个traits,这些traits 可以作为部分或者全部成员被定义在其他traits中。
<?phptrait hello {public function sayhello() {echo 'hello ';}}trait world {public function sayworld() {echo 'world!';}}trait helloworld {use hello, world;}class myhelloworld {use helloworld;}$o = new myhelloworld();$o->sayhello();$o->sayworld();?>
以上例程会输出:
hello world!
7)抽象trait成员
为了在类中强制实现某些方法,可以在traits中使用抽象方法。
例如:
<?phptrait hello { public function sayhelloworld() { echo 'hello '.$this->getworld(); } abstract public function getworld();}class myhelloworld { private $world; use hello; public function __construct($world) { $this->world = $world; } public function getworld() { return $this->world; }}/** * 这里用到了5.4新功能 类实例化解引用操作 * (new class())->method(); */(new myhelloworld('arvin'))->sayhelloworld();?>
该实例输出:
hello arvin
8)静态trait成员
在traits中不能定义static 静态变量,但是可以定义在tratis的函数中。tratis中同样可以定义静态函数。
<?phptrait counter { public function inc() { static $c = 0;//静态变量 $c += 1; echo "$c\n"; } /** * 静态方法 */ public static function dosomething() { echo 'doing something'; }}class c1 { use counter;}(new c1())->inc(); // echo 1c1::dosomething();?>输出为:1doing something
9) traits 定义属性
如果在一个trait中定义了一个属性,则在引用该trait的类中不能定义同名的属性,如果该类中定义有和trait中已定义属性具有相同的名字和访问可见性,则是一个e_strict 提示,否则抛出语法错误。
<?phptrait propertiestrait { public $x = 1; public $y = 2;}class propertiesexample { use propertiestrait; public $x = 1; //public $y = 3;}$example = new propertiesexample;echo $example->x, $example->y;?>
输出:
12
在最后贴上php5.4.0部分新功能changelog:
added short array syntax support ([1,2,3]), see upgrading guide for full details.added binary numbers format (0b001010).added support for class::{expr}() syntax.added support for traits.//本文的主要内容added closure $this support back.added array dereferencing support.//数组解引用支持,上文中有实例added callable typehint.added indirect method call through array. #47160.added dtrace support.//传说dtrace是一个性能分析工具,可以跟踪出函数调用点,返回点等数据added class member access on instantiation (e.g. (new foo)->bar()) support.//类新实例解引用操作,上文中有实例
本文旨在抛砖引玉,希望大家一起继续探究php5.4的新功能。^_^
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product