Traits is a new method added in 5.4 to achieve code reuse.
php is a single inheritance language. We cannot extend multiple base classes in one class to achieve code reuse like Java. Now Traits can solve this code reuse problem. It allows developers to use multiple base classes in different classes. Implement code reuse in classes.
Traits and classes are semantically defined to reduce the complexity of the code and avoid the problem of multiple inheritance.
Traits are similar to classes, but are only used to provide a set of functions in a unified and fine-grained manner. They cannot be instantiated inside Traits, that is, there is no class-like constructor __construct(). Traits acts as an extension of PHP's traditional inheritance and implements horizontal integration; therefore, inheritance is no longer needed in application classes.
1) How to use
Use the keyword 'use' to reference Traits in the class. Multiple Traits are separated by ','.
The example code is as follows:
2) Priority
The member functions in the base class will be overridden by the functions in Traits, and the member functions in the current class will override the functions in Traits.
sayHello(); $oe = new MyHelloWorldExt(); $oe->sayHello(); echo "\n"; $oe->shortArray();
Output:
Hello World! A first
3) Multiple Traits
Multiple Traits can be added to the declaration of a class, and multiple Traits are separated by ",".
sayHello(); $o->sayWorld(); ?>
Output result:
Hello World
4) Multiple Traits Conflict
If two traits added to the same class have the same function name and are not processed explicitly, an error will be generated.
In order to resolve the conflict between two functions with the same name in Tratis in the same class, you need to use the insteadof operator to select the correct function.
Because of the uniqueness and exclusivity of methods, the 'as' operator is allowed to be used after conflicting functions to resolve internal conflicts.
In the above example, Talker uses Traits A and B, so there is a conflict between the same function names in both.
alker defines that smallTalk is taken from Traits B and bigTalk is taken from Traits A.
Aliased_Talker uses the as operator to ensure that bigTalk in Traits B is implemented through the alias talk.
5) Change function access permissions
We can use as syntax to change the access permission attributes of functions in Traits.
6) Traits form new Traits
Just like many classes, Traits can be used in classes, and Traits can be used in Traits. One or more Traits can be defined in one Traits, and these Traits can be defined in other Traits as part or all members.
sayHello(); $o->sayWorld(); ?>
The above routine will output:
Hello World!
7) Abstract Trait members
In order to enforce certain methods in a class, you can use abstract methods in Traits.
For example:
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(); ?>
This example output:
Hello Arvin
8) Static Trait members
static static variables cannot be defined in Traits, but they can be defined in Tratis functions. Static functions can also be defined in Tratis.
inc(); // echo 1 C1::doSomething(); ?> 输出为: 1 Doing something
9) Traits define attributes
If an attribute is defined in a trait, an attribute with the same name cannot be defined in the class that references the trait. If the attribute defined in the class has the same attribute as the attribute defined in the trait Same name and access visibility, an E_STRICT hint, otherwise a syntax error is thrown.
x, $example->y; ?>
Output:
12
Post some of the new features of php5.4.0 changelog at the end:
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.//类新实例解引用操作,上文中有实例
This article is intended to attract others, and I hope everyone will continue to explore the new features of php5.4. ^_^