I have a parent class, let’s say class main { ... }, and an extension of it, let’s call it class extension extends main { ... }.
My question is, how would I build another class, called class messages { ... }, which I can use inside the main class and the extended class of main, extension ? Besides the way I know, calling the class messages like this :
$messages = new messages;
$messages->someMethod();
Is there another way without having to do new ... to make the main and extension class inherit the methods inside the messages class ?
AFAIK, PHP does not support multiple inheritance, as others OOP languages do.
So, no, there is NOT another way.
And yes, you should create a property and instantiate the object inside the
mainclass…However, there are alternatives to simulate a fake multiple inheritance.
An alternative would be: https://stackoverflow.com/a/356431/370290 – But I don’t recommend this (even the own author doesn’t).
Another alternative: https://stackoverflow.com/a/358562/370290 – IMHO, as weird as the previous one. 🙂
And as of PHP 5.4.0 you can also use traits to achieve a “multiple inheritance” effect: http://php.net/manual/en/language.oop5.traits.php – This is very new at the moment.