A类对象随意(并非总是)继承B类行为,同时保留自己的行为。 可能?

So, I have two classes. I am wondering if I can have two types of objects of Class A:

  1. normal objects of Class A,
  2. objects of class A that inherit Class B's behavior whilst retaining their own,

without creating a third Class.

Is this possible?

Class A in its basic form does not have all the features of Class B, therefore extending Class B is not an option.

I am using PHP btw.

So, what I discovered was that if you want a certain behavior to be shared between different classes (horizontally), this is possible in PHP 5.4+ with the use of traits.

For example, you can define a trait called 'Driving' and various types of Driver actors can implement it.

The classes implementing traits will inherit all the properties and methods of the trait, and those properties and methods will appear as part of the class even when using reflection.

Here's a useful link that explains it well.