PHP's Trait features

Source: Internet
Author: User

Since PHP 5.4.0, PHP has implemented a code reuse method called trait.
    • Trait is a code reuse mechanism that is prepared for PHP-like single-inheritance languages. Trait to reduce the limitations of single-inheritance languages, developers are free to reuse method in separate classes within different hierarchies. The semantics of Trait and class combinations define a way to reduce complexity, avoiding traditional multi-inheritance and Mixin class-related typical problems.
    • Trait is similar to Class, but only designed to combine functionality in a fine-grained and consistent way. cannot be instantiated by trait itself. It adds a combination of horizontal features to the traditional inheritance, meaning that no inheritance is required between several classes of the application.
<?php//Log Class class logger{//read log information public function log (string $message, int $level) {echo "[message]:{$m Essage} ".        Php_eol; echo "[level]:{$level}".    Php_eol;    }}//Extended Log function trait loggable{protected $logger; /** * log * @param demologger $logger */Public Function Setlogger (logger $logger) {$this->l    Ogger = $logger;  /** * Read log * @param string $message * @param int $level */Public Function GetLog (string $message,    int $level) {$this->logger->log ($message, $level); } Public Function Test () {echo ' Trait test '.    Php_eol;    }}//base class class base{public static $className = ' base '; Public Function test () {echo static::getclassname (). ' Test '.    Php_eol;        }//Gets the class name public static function GetClassName (): string {//return-Self:: $className; return static:: $className;//static delay static binding}}class Foo extends base{public static $className =' Foo '; Use loggable;} $foo = new Foo; $foo->setlogger (new Logger); $foo->getlog (' Trait Works ', 1);//print log information $foo->test (); Trait test
  • Analysis here $foo->test ()
    • The Foo class uses the use loggable to extend the Foo class to increase the logging function;
    • The Trait loggable class contains the test () method;
    • The Foo class inherits the base class, where the base class contains test ();
    • So the question is: $foo->test () is the call inherited from the parent class test () or the test () in the Trait class?
Trait the function of the current class overrides trait's function with the same name, trait overrides the parent class's function with the same name (use trait is equivalent to the current class directly overwrites the parent class with the same name function)

Therefore, the method in the trait class is called by the $foo->test ()

PHP's Trait features

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: 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.