There are several classes: A, B, C, D, where A is the main one, and B, C, D I want to expand class A with them, without recreating it, but simply supplementing it, is it possible? so that class A is a set of A, B, C, D? (i.e. code D uses C functions, C uses functions A, B).

    2 answers 2

    PHP 5.4+: traits , so-called language-level mixing.
    PHP 5.3-: behaviors through __set, __get, __call, and other magic that look for a non-found property or method in an array of object behaviors that are added to it in runtime. This lived Yii 1.x, although in the second they also seem to be preserved.

    In any case, you almost certainly do something wrong, and you should ask a solution for a specific task. I now have up to 90% of classes that do not have a parent at all, while the others already have a parent, and there is no talk about multiple inheritance at all. If a class performs a specific function (single responsibility principle), then it will never need to inherit from two classes, each of which performs its specific function.

      There is no plural heritage in PHP. Therefore, only one parent.

      • Well, perhaps not through inheritance but through a crutch?)))) For example, frames include class by class (CI for example), which is necessary according to the type of such a scheme. - Manitikyl
      • @Manitikyl, you can not expand the extension of the class. You can of course be perverted with __call and class loading, but is it necessary? Practice shows that if you need a crutch - redo the application (unless, of course, the crutch is not for the work of the environment itself (wow, IE!)). - user31688