Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple Inheritance in PHP or is there a better way?
    primarykey
    data
    text
    <p>So I am having a hard time getting around this concept of PHP not having multiple inheritance.</p> <p>I have an idea, I drew out where I want to do this:</p> <pre><code>Loop extends LoopHelper &lt;-&gt; [ Single, Query, Page] * **LoopHelper would extend each class in the brackets. </code></pre> <p><strong>Note</strong>: (*) 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)</p> <p>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 (<a href="http://www.php.net/manual/en/language.oop5.inheritance.php#102142" rel="nofollow">There is this hack</a>), 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 <code>Single.php</code>, <code>Query.php</code> and <code>Page.php</code> with out having to instantiate them ...</p> <p>Is there an efficient way to do this? or am I crazy?</p> <p>What I was doing in <code>Loop</code> was creating an <code>initilize_helper()</code> 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 <code>Single</code>, <code>Query</code> and <code>Page</code> classes to the <code>Loop</code> (since <code>Single</code>, <code>Query</code> and <code>Page</code> extend from <code>LoopHelper</code>)</p> <p>There are some obvious flaws with this idea I drew out, things I want but don't think I can get.</p> <p><code>Loop($options)</code> passes options to <code>LoopHelper</code> which then would distribute them out to each "extended class" (note the quotes), in turn (note the &lt;-> in the example) Loop can call functions from Single, Query or Page via <code>$this</code></p> <p>Issues, <strong>no multiple inheritance</strong>, <strong>No language (I know of, please correct me if I am wrong) allows two way inheritance</strong>, so <code>LoopHelper extends from Single, Single extends from LoopHelper</code></p> <p>At the end of the day, what I expect is:</p> <p><code>Loop</code> Should be able to call any of <code>Single</code>, <code>Query</code> or <code>Page</code> class methods, While making sure that, <em>for instance if you pass in an array of options</em>, that those options get passed through some kind of "bridge" to each of the three classes, nothing should depend on anything <strong>but</strong> the "bridge", where the "bridge" can be multiple classes if needed.</p> <pre><code>//Loop --&gt; ($options) -&gt; __Some_Kind_Of_Bridge __ --&gt; ($options) -&gt; [Single, Query, Page] class Loop extends Some_Kind_Of_Bridge{ public function call_methods(){ $this-&gt;some_page_method(); $this-&gt;some_query_method(); $this-&gt;some_single_method(); } } $loop = new Loop(array('some', 'values')); $loop-&gt;call_methods(); </code></pre> <p>You'll note I am not creating a constructor in <code>Loop</code>, 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.</p> <p>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.</p> <p>Whats the easiest way to solve this problem that is both OO and clean? (little to no coupling).</p> <p>Are their some patterns I should be looking at?</p> <p><strong>UPDATE</strong></p> <p>This post does not reflect on <a href="http://ca.php.net/interfaces" rel="nofollow">interfaces</a> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload