Читать книгу PHP This! A Beginners Guide to Learning Object Oriented PHP - Michelle Gosney - Страница 12

Horizontal Inheritance – Using Traits

Оглавление

PHP does not support multiple inheritance; a class can only extend a single base class. What happens if you need code from more than one class? Until recently, developers had to accomplish this in a crude manner by copying and pasting code from additional classes that was needed outside of its base class. Now this can be done by way of a new feature introduced in PHP5.4 called a trait that provides a means of horizontal inheritance.

A trait looks like a class in the way it is structured but it cannot be instantiated. Properties and methods can be defined within a trait structure but they can only be used by a class that incorporates them with the use keyword which is called within the class that uses it, that is, it goes inside the curly braces. It isn’t called in the class definition outside the curly braces like interfaces and the extending class (we will get to interfaces in Chapter 5).


PHP This! A Beginners Guide to Learning Object Oriented  PHP

Подняться наверх