_php examples of trait characteristics and functions in PHP

Source: Internet
Author: User
Tags modifiers traits

Since PHP 5.4.0, PHP implements a code reuse method, called traits.

Traits is a code reuse mechanism prepared for a single inheritance language similar to PHP. Trait to reduce the limitations of single inheritance languages, developers are free to reuse method sets in separate classes within different hierarchies. The semantics of Traits and class combinations define a way to reduce complexity and avoid typical problems associated with traditional multiple inheritance and mixed-Class (Mixin).

Trait is similar to a class, but is intended only to combine functionality in fine-grained and consistent ways. Trait cannot be instantiated by itself. It adds a combination of horizontal attributes for traditional inheritance, which means that members of the application class do not need to inherit.

Trait is added in PHP5.4, and it is neither an interface nor a class. The main purpose is to solve the limitation of single inheritance language. is a solution to multiple inheritance in PHP. For example, it would be a hassle to inherit two Abstract classes at the same time, Trait to solve the problem. It can be added to one or more existing classes. It declares what a class can do (it indicates its interface characteristics) and also contains a specific implementation (indicating its class characteristics)

Simple to use

First of all, of course, declare a trait,php5.4 add the Trait keyword

Trait first_trait {
function First_method () {/* code here/}
function Second_method () {/* code here/}
}

Also, if you want to use the Trait in Class, use the Using keyword

Class First_class {
//Note this line, declare using first_trait use
first_trait;
}
$obj = new First_class ();
Executing the method from trait
$obj->first_method ();//valid
$obj->second_method ();//Valid

Using multiple Trait

Multiple Trait can be used in the same Class

Trait first_trait
{
function First_method () {echo ' method ';}
}
Trait second_trait {
function Second_method () {echo ' method ';}
}
Class First_class {
//now using the more than one trait
the use first_trait, second_trait;
}
$obj = new First_class ();
Valid
$obj->first_method ();//Print:method
//Valid
$obj->second_method ();//Print:method

Nesting between Trait

Also, Trait can be nested between each other, such as

Trait first_trait {
function First_method () {echo ' method ';}
}
Trait second_trait {use
first_trait;
function Second_method () {echo ' method ';}
}
Class First_class {
//now using use 
second_trait;
}
$obj = new First_class ();
Valid
$obj->first_method ();//Print:method
//Valid
$obj->second_method ();//Print:method

Trait abstract Methods (abstract method)

We can declare the abstract method that needs to be implemented in Trait so that the Class that uses it must implement it

Trait first_trait {
function First_method () {echo ' method ';}
The modifier can be added here to show that the calling class must implement its
abstract public Function Second_method ();
}
Class First_method {use
first_trait;
function Second_method () {/
* Code here/
}
}

Trait conflict

The simultaneous use of multiple Trait will inevitably conflict, which requires us to solve. PHP5.4 from syntax to related keyword syntax: insteadof and AS, usage see

Trait first_trait {
function first_function () {echo "from-a- 
trait";
}
}
Trait second_trait {
//The name here is the same as first_trait, there will be conflicting
function first_function () { 
echo "from second trait";
}
class First_class {
use first_trait, second_trait {
//declared here to replace the first_function with first_trait First_trait::first_function insteadof second_trait declared in c14/>//second_trait
;
} 
$obj = new First_class ();
Output:from-Trait
$obj->first_function ();

The above is a few Trait more basic use, more detailed can refer to the Official Handbook. Here is a summary of the points to note:

Trait overrides the parent class method of calling class inheritance

Trait cannot use new instantiation as Class

A single Trait can be composed of multiple Trait

In a single Class, you can use multiple Trait

Trait support modifiers (modifiers), such as final, static, abstract

We can use the insteadof and as operators to resolve conflicts between Trait

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.