Home  >  Article  >  Backend Development  >  Detailed explanation of PHP code reuse mechanism examples

Detailed explanation of PHP code reuse mechanism examples

小云云
小云云Original
2018-03-13 13:27:191857browse

1.Trait is a code reuse mechanism prepared for single inheritance languages ​​like PHP. Traits are designed to reduce the limitations of single-inheritance languages ​​and allow developers to freely reuse methods in independent classes within different hierarchies.

2. Members inherited from the base class will be overridden by members inserted by the trait.

3. Code example:

trait T{    public function run()    
{        parent::run();        
echo 'Trait:'.__CLASS__.'<br>';    
}}class P{    public function run()    
{        echo 'Class:'.__CLASS__.'<br>';    
}}
class C extends P{    
use T;}$c = new C();$c->run();
//输出结果
//Class:P
//Trait:C

Related recommendations:

How to use traits to achieve PHP code reuse

php code reuse traits simple definition and usage introduction

Examples of using traits to implement code reuse in PHP_PHP tutorial

The above is the detailed content of Detailed explanation of PHP code reuse mechanism examples. For more information, please follow other related articles on the PHP Chinese website!

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