Trait features of PHP5.4

Source: Internet
Author: User
Tags traits
The Trait feature of PHP5.4 is a new feature in PHP5.4 and a solution for PHP multi-inheritance. For example, it is very troublesome to inherit two Abstract classes at the same time. Trait is designed to solve this problem.

Easy to use

First, of course, we declare a Trait. the trait keyword is added in PHP5.4:

trait first_trait {    function first_method() { /* Code Here */ }    function second_method() { /* Code Here */ }}

If you want to use this Trait in the Class, use the use keyword:

Class first_class {// pay attention to this line and declare to use first_trait;} $ obj = new first_class (); // Executing the method from trait $ obj-> first_method (); // valid $ obj-> second_method (); // valid

Use multiple Trait

You can use multiple Trait instances in the same Class:

trait first_trait{    function first_method() { echo "method1"; }}trait second_trait {    function second_method() { echo "method2"; }}class first_class {    // now using more than one trait    use first_trait, second_trait;}$obj= new first_class();// Valid$obj->first_method(); // Print : method1// Valid$obj->second_method(); // Print : method2

Nesting between Trait

Meanwhile, Trait can also be nested with each other, for example:

trait first_trait {    function first_method() { echo "method1"; }}trait second_trait {    use first_trait;    function second_method() { echo "method2"; }}class first_class {    // now using     use second_trait;}$obj= new first_class();// Valid$obj->first_method(); // Print : method1// Valid$obj->second_method(); // Print : method2

Abstract Method of Trait)

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

Trait first_trait {function first_method () {echo "method1" ;}// here you can add modifiers, indicating that the call class must implement the abstract public function second_method ();} class first_method {use first_trait; function second_method () {/* Code Here */}}

Trait conflict

When multiple Trait nodes are used at the same time, they will inevitably conflict with each other. we need to solve this problem. PHP5.4 introduces the related keyword syntax from the syntax aspect: insteadof and as. for usage, see:

Trait first_trait {function first_function () {echo "From First Trait" ;}} trait second_trait {// The name here is the same as first_trait, and there will be conflicting function first_function () {echo "From Second Trait" ;}} class first_class {use first_trait, second_trait {// declare here to use first_trait's first_function to replace // first_trait declared in second_trait :: first_function insteadof second_trait; }}$ obj = new first_class (); // Output: From First Trait $ obj-> first_function ();

Notes

The above are some basic usage of Trait. For more details, refer to the official manual. Here are some notes:

Trait will overwrite the parent Class method that calls Class inheritance. Trait cannot use new to instantiate a single Trait. it can be composed of multiple Trait in a single Class, you can use multiple Trait support modifiers, such as final, static, and abstract. we can use the insteadof and as operators to solve Trait conflicts.

Frankly speaking, I saw at first glance that Trait had no liking for it. New features brought about by PHP5 are sufficient, and developers are somewhat overwhelmed.

At the same time, Trait is more like a programmer's "syntactic sugar". However, its convenience may cause huge risks. For example, Trait can call members in the class:

trait Hello {    public function sayHelloWorld() {        echo 'Hello'.$this->getWorld();    }    abstract public function getWorld();}class MyHelloWorld {    private $world;    use Hello;    public function getWorld() {        return $this->world;    }    public function setWorld($val) {        $this->world = $val;    }}

At the same time, Trait has no effect on methods implemented in the class:

trait HelloWorld {    public function sayHello() {        echo 'Hello World!';    }}class TheWorldIsNotEnough {    use HelloWorld;    public function sayHello() {        echo 'Hello Universe!';    }}$o = new TheWorldIsNotEnough();$o->sayHello(); // echos Hello Universe!

Why does Trait appear? Some of my friends have an interesting answer, but it makes no sense:

Because php does not have the mechanism of javascript scope chain, it is impossible to bind the function to the class. I used to think that the closure of php 5.3 can do this. In the end, I realized that the scope design was not allowed to do this.


But in other words, taking the analogy of interface and Trait, it is obvious that Trait has more convenience (although the two cannot completely replace each other ).

However, it is clear that Trait is still in the testing stage, and its future is more visible than other new features of PHP5, but this feature may change the way PHP5 inherits in the future.

Because, I personally believe that PHP's function chain design will sooner or later be "more like JavaScript", even if it will be in the distant PHP6.

Reference link http://php.net/manual/en/language.oop5.traits.php https://wiki.php.net/rfc/traits http://en.wikipedia.org/wiki/Trait_%28computer_programming%29 http://bbs.phpchina.com/thread-210870-1-1.html http://scg.unibe.ch/research/traits/ http://walu.sinaapp.com /? P = 60 http://www.phppan.com/2011/07/mixin-and-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.