1

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 ?

Roland
  • 9,321
  • 17
  • 79
  • 135
  • 1
    I'm not sure are you looking for `virtual` or `static` method? Take a look in manual http://php.net/manual/en/language.oop5.static.php – Vyktor Feb 05 '12 at 09:47

4 Answers4

3

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 main class...

class main {
    public $messages;  // may be "protected" or "private" instead

    public __construct()
    {
        $this->messages = new messages();
    }

    public do_something()
    {
        $this->messages->do_something_else();
    }
}

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.

Community
  • 1
  • 1
J. Bruni
  • 20,322
  • 12
  • 75
  • 92
  • OK, so you suggest initiating the `messages` class inside the `_construct` method and binding the methods from the `messages` class to the `public $messages` variable ? This would mean I can call any method from `messages` class whenever I need just by `$this->messages->do_something_else();`. That's a good idea, as @Nanne suggested too. Because I'm already using the `_construct` method to make a connection with the db. – Roland Feb 05 '12 at 10:02
  • @Roland: yes, yes. This would be the simple, straight, effective and normal way to do it. The other approaches serve only if you desperately need a different kind of stuff. This is how I understand this subject. BTW, the technical name for this is **composition** - see http://stackoverflow.com/a/477198/370290 – J. Bruni Feb 05 '12 at 10:05
  • Maybe **trait** would be the way to go, but it is too recent. (I have not used it myself yet.) The stable PHP 5.4.0 version has not even been released at the present moment. – J. Bruni Feb 05 '12 at 10:10
  • Nice, `traits` looks so good. TO bad that it's supported only as of PHP 5.4.0, because a lot of servers still run on much older versions. I for example, am using a service that runs on PHP 5.2 or 4.3, so `traits` is out of question for me, even though it looks so good :) – Roland Feb 05 '12 at 10:13
1

The problem you seem to have is that you can't do multiple inheritance (class YourClass extends main, messages).

The common feeling is that if you need multiple enheritance, you're doing something wrong in your design.

Every class is responsible for a single thing. A "extension" in this case "IS A" "main", but it is not a "messages", so it should not be a child of that. IF you need messaging capability, there is no 'shame' at all in just calling it like you suggest: you get yourself a nice object that knows how to message, and play with that. There is no real need to do it differently.

If you're looking for alternatives (which you really don't need as far as I can see!) you could make it a class with a bunch of static methods, and just call it like messages::someMethod(), but I think that would be considered an anti-pattern in this case.

Just go with it: messages are created by an object of type message. So you make one, and call the function. In the end, if you ever need big changes (database connection, logging, etc etc) for you messaging, you can do this all in your nice and cosy messaging class. Everyone happy :)

Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108
Nanne
  • 64,065
  • 16
  • 119
  • 163
1

You can't extend multiple classes and to exten the main class .. only a good thing if you extend from an abstract class.

But what you could do is add it in the construct of your main class like this:

//member variable for class main
public $_message = null;

public function __construct()
{
    $this->_message = new Message();
}

Then whenever you need the message class just call $this->_message + the method you need (eg: $this->_message->addMessage())

don't forget to add this in you subclass:

public function __construct()
{
    parent::__construct();
}
Christophe
  • 4,798
  • 5
  • 41
  • 83
0

You should create a class inside a class. Just like in this question.

Then, you can use $this->someclass->function.

Note: construct needs to be $this->someclass = new Whatever() too.

Community
  • 1
  • 1
axiomer
  • 2,088
  • 1
  • 17
  • 26