Looking at the PHP framework today, I saw the Trait part. I haven't seen it before, so I checked it out out of curiosity.
The PHP manual talks about solving the problem of multiple inheritance. But in general object-oriented languages, multiple inheritance is solved through interfaces, and PHP also has interfaces. It seems that Trait and Interface are in conflict, but if we take a closer look at the usage of Trait and Interface, there are actually essential differences. Interface defines methods, which are implemented by inherited classes, while Traits implement some specific methods that need to be reused. Therefore, I think Traits are not so much code reuse to solve PHP's object-oriented multiple inheritance, but rather code fragments introduced by repeated code in PHP! ……^ _ ^
The above is the detailed content of About Trait issues in PHP. For more information, please follow other related articles on the PHP Chinese website!