1

So I am having a hard time getting around this concept of PHP not having multiple inheritance.

I have an idea, I drew out where I want to do this:

Loop extends LoopHelper <-> [ Single, Query, Page] *
**LoopHelper would extend each class in the brackets.

Note: (*) The idea is: any class in the brackets extends from loop helper where loop helper extends from any class in the brackets (obviously impossible, please read on)

The idea is that Loop could abstract out a lot of its logic. But since we all know that PHP does NOT support multiple inheritance (There is this hack), I am looking for another way to do this, so that Loop could go through one (or more) parent(s) to be able to get access to Single.php, Query.php and Page.php with out having to instantiate them ...

Is there an efficient way to do this? or am I crazy?

What I was doing in Loop was creating an initilize_helper() method that would instantiate all three of these classes, into three separate class level variables that could then be used call on the method you needed. (this is both unclean and couples the Single, Query and Page classes to the Loop (since Single, Query and Page extend from LoopHelper)

There are some obvious flaws with this idea I drew out, things I want but don't think I can get.

Loop($options) passes options to LoopHelper which then would distribute them out to each "extended class" (note the quotes), in turn (note the <-> in the example) Loop can call functions from Single, Query or Page via $this

Issues, no multiple inheritance, No language (I know of, please correct me if I am wrong) allows two way inheritance, so LoopHelper extends from Single, Single extends from LoopHelper

At the end of the day, what I expect is:

Loop Should be able to call any of Single, Query or Page class methods, While making sure that, for instance if you pass in an array of options, that those options get passed through some kind of "bridge" to each of the three classes, nothing should depend on anything but the "bridge", where the "bridge" can be multiple classes if needed.

//Loop --> ($options) -> __Some_Kind_Of_Bridge __ --> ($options) -> [Single, Query, Page]
class Loop extends Some_Kind_Of_Bridge{

    public function call_methods(){
        $this->some_page_method();
        $this->some_query_method();
        $this->some_single_method();
    }
}

$loop = new Loop(array('some', 'values'));
$loop->call_methods();

You'll note I am not creating a constructor in Loop, php will, by default use the parent classes constructor if you don't create one. Back practice I know, but this is just an example.

Dependency injection looks promising but is it what I really need - How could it be implemented in a case like this? - Nothing depends on anything but the bridge class.

Whats the easiest way to solve this problem that is both OO and clean? (little to no coupling).

Are their some patterns I should be looking at?

UPDATE

This post does not reflect on interfaces who can extend multiple class, this is related to clas->class inheritance. If interfaces are the way to go please explain how i could implement them in this situation.

5
  • It should be noted that interfaces can extend multiple classes. Commented Oct 7, 2013 at 19:04
  • Updated the OP to reflect this Commented Oct 7, 2013 at 19:06
  • 1
    Maybe try traits? I would figure out a way to use interfaces though and inject the looper interfaces into your function. Commented Oct 7, 2013 at 19:09
  • Perhaps composition would be a better approach for what you're trying to achieve. en.wikipedia.org/wiki/Composition_over_inheritance Commented Oct 7, 2013 at 19:09
  • (In response to your "please correct me if I'm wrong") - C++ does support multiple inheritance. Commented Sep 15, 2014 at 18:47

1 Answer 1

1

If you use php >= 5.4, you can use traits:

class Loop {
  use Single, Query, Page;
}

EDIT: for older versions of php, what you are looking for is called "mixins", which is an emulation of traits. There are several ways to do that, none of which is perfect, but you will surely find something that fits your needs if you search for this keyword.

Sign up to request clarification or add additional context in comments.

2 Comments

Due to the classes I am building - they are used for WordPress ... And some people might be using PHP 4.3 (WordPress min requirement) So I would need a fall back. I cannot lock out a group of people using free hosting or hosting services with less then 5.4
@LogicLooking, the minimum requirement for WordPress is PHP 5.2.4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.