How to implement new traits of code reuse in PHP

coldplay.xixi
Release: 2023-04-09 11:24:01
forward
1679 people have browsed it

How to implement new traits of code reuse in PHP

I came into contact with traits while reading the yii2 source code, so I studied it and wrote a blog to record it.

Since PHP 5.4.0, PHP has implemented a method of code reuse called traits.

Traits is a code reuse mechanism prepared for single inheritance languages like PHP. Traits are designed to reduce the constraints of single-inheritance languages and allow developers to freely reuse method sets in independent classes within different hierarchies. The semantics of traits and class composition define a way to reduce complexity and avoid the typical problems associated with traditional multiple inheritance and mixins.

Trait is similar to a class, but is only designed to combine functionality in a fine-grained and consistent way. Trait cannot be instantiated by itself. It adds a combination of horizontal features to traditional inheritance; that is, members of application classes do not need to be inherited.

Trait example

The code is as follows:

Copy after login

Priority

Members inherited from the base class are inserted by trait covered by members. The order of precedence is that members from the current class override the trait's methods, and the trait overrides the inherited methods.

Priority order example

The code is as follows:

sayHello(); ?>
Copy after login

The above routine will output: Hello World!

SayWorld where members inherited from the base class are inserted Covered by the sayHello method in Trait. Its behavior is consistent with the methods defined in the MyHelloWorld class. The order of precedence is that methods in the current class override trait methods, which in turn override methods in the base class.

Another example of priority order

The code is as follows:

sayHello(); ?>
Copy after login

The above routine will output: Hello Universe!

Multiple traits

are separated by commas and multiple traits are listed in the use statement. They can all be inserted into a class.

Examples of usage of multiple traits

The code is as follows:

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

The above routine will output: Hello World!

Conflict resolution

If two traits insert a method with the same name, a fatal error will occur if the conflict is not resolved explicitly.

In order to resolve the naming conflict of multiple traits in the same class, you need to use the insteadof operator to explicitly specify which of the conflicting methods to use.

The above method only allows to exclude other methods. The as operator can introduce one of the conflicting methods under another name.

Example of conflict resolution

The code is as follows:

Copy after login

In this example Talker uses traits A and B. Since A and B have conflicting methods, they define using smallTalk from trait B and bigTalk from trait A.

Aliased_TalkerUse the as operator to define talk as an alias of B's bigTalk.

Modify the access control of the method

Using the as syntax can also be used to adjust the access control of the method.

An example of modifying the access control of a method

The code is as follows:

Copy after login

Composed from trait to trait

Just like Just as classes can use traits, other traits can also use traits. By using one or more traits when a trait is defined, it can combine some or all members of other traits.

Example of composing trait from trait

The code is as follows:

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

The above routine will output: Hello World!

Abstract of Trait Members

In order to enforce requirements on the classes used, traits support the use of abstract methods.

Indicates an example of mandatory requirements through abstract methods

The code is as follows:

getWorld(); } abstract public function getWorld(); } class MyHelloWorld { private $world; use Hello; public function getWorld() { return $this->world; } public function setWorld($val) { $this->world = $val; } } ?>
Copy after login

Static member of Trait

Traits can be defined by static members and static methods.

Example of static variables

The code is as follows:

inc(); // echo 1 $p = new C2(); $p->inc(); // echo 1 ?>
Copy after login

Example of static method

The code is as follows:

Copy after login

Examples of static variables and static methods

The code is as follows:

Copy after login

Attributes
Trait can also define attributes.

Example of defining attributes

The code is as follows:

x; ?>
Copy after login

If a trait defines an attribute, that class will not be able to define an attribute with the same name, otherwise a mistake. If the property's definition in the class is compatible with its definition in the trait (same visibility and initial value) then the error level is E_STRICT, otherwise it is a fatal error.

Conflict examples

The code is as follows:

Copy after login

Differences in Use

Examples of different uses

The code is as follows:

 
Copy after login

The first use is use Foo\Test for namespace, and what is found is \Foo\Test. The second use is to use a trait. Found\Foo\Bar\Foo\Test.

__CLASS__and__TRAIT__
__CLASS__Returns the class name of the use trait, __TRAIT__ returns the trait name

Example The code is as follows

The code is as follows:

testMethod(); //Class: BaseClass //Trait: TestTrait
Copy after login

Trait singleton

The example is as follows

The code is as follows:

name = 'foo'; } } class bar { use singleton; private function __construct() { $this->name = 'bar'; } } $foo = foo::getInstance(); echo $foo->name; $bar = bar::getInstance(); echo $bar->name;
Copy after login

Call trait method

Although it is not obvious, if the trait method can be defined as a static method in a normal class, it can be called

实例如下

代码如下:

Copy after login

相关学习推荐:PHP编程从入门到精通

The above is the detailed content of How to implement new traits of code reuse in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.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!