An article to talk about the new features Traits of php5.4

PHPz
Release: 2022-07-27 12:36:09
forward
1459 people have browsed it

An article to talk about the new features Traits of php5.4

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:

Copy after login

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();
Copy after login

Output:

Hello World! A first
Copy after login

3) Multiple Traits

Multiple Traits can be added to the declaration of a class, and multiple Traits are separated by ",".

sayHello(); $o->sayWorld(); ?>
Copy after login

Output result:

Hello World
Copy after login

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.

Copy after login

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.

Copy after login

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(); ?>
Copy after login

The above routine will output:

Hello World!
Copy after login

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(); ?>
Copy after login

This example output:

Hello Arvin
Copy after login

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
Copy after login

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; ?>
Copy after login

Output:

12
Copy after login

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.//类新实例解引用操作,上文中有实例
Copy after login

This article is intended to attract others, and I hope everyone will continue to explore the new features of php5.4. ^_^

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!