<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:series="http://unfoldingneurons.com/"
		>
<channel>
	<title>Comments on: Where Multiple Levels Of Inheritance Will Kill You</title>
	<atom:link href="http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/</link>
	<description>The personal blog of Brandon Savage. Contains entries of a personal and professional nature focusing on PHP, Apple, LAMP, MySQL and Washington, DC.</description>
	<lastBuildDate>Fri, 03 Feb 2012 19:36:33 -0500</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Timo Reitz</title>
		<link>http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/#comment-1864</link>
		<dc:creator>Timo Reitz</dc:creator>
		<pubDate>Mon, 02 Nov 2009 16:29:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=472#comment-1864</guid>
		<description>I think the article describes two completely different problems.

The first one being deep inheritance trees. Those deep trees tie all the classes within a hierarchy together, so if you change one, you often have to look at the others. So avoid these. Sometimes it can be better to use composition instead of inheritance, even if inheritance would not be a logical flaw (like the fruit-salad example). So Apple would not inherit from Fruit, but instead wraps a Fruit and delegates some method calls to it. I think this really is what &quot;Favor composition over inheritance&quot; is about.

The second problem is what you mentioned - don&#039;t confuse &quot;is-a&quot; with &quot;has-a&quot;. This is not what I think &quot;Favor composition over inheritance&quot; is about, because using inheritance if you have &quot;has-a&quot; relationship is not a matter of choice, but simply wrong!</description>
		<content:encoded><![CDATA[<p>I think the article describes two completely different problems.</p>
<p>The first one being deep inheritance trees. Those deep trees tie all the classes within a hierarchy together, so if you change one, you often have to look at the others. So avoid these. Sometimes it can be better to use composition instead of inheritance, even if inheritance would not be a logical flaw (like the fruit-salad example). So Apple would not inherit from Fruit, but instead wraps a Fruit and delegates some method calls to it. I think this really is what &#8220;Favor composition over inheritance&#8221; is about.</p>
<p>The second problem is what you mentioned &#8211; don&#8217;t confuse &#8220;is-a&#8221; with &#8220;has-a&#8221;. This is not what I think &#8220;Favor composition over inheritance&#8221; is about, because using inheritance if you have &#8220;has-a&#8221; relationship is not a matter of choice, but simply wrong!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Samuel Folkes</title>
		<link>http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/#comment-874</link>
		<dc:creator>Samuel Folkes</dc:creator>
		<pubDate>Mon, 10 Aug 2009 18:17:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=472#comment-874</guid>
		<description>This article was well written and the examples were crystal clear. I really don&#039;t see what the fuss is about with some of the comments above. Levels of inheritance simply MUST follow a logical hierarchy or else you are setting yourself up for problems. The only (minor) gripe I have with this article is that the problem(s) outlined here are not exactly direct results of using multiple levels of inheritance but more results of bad design and failure to understand correctly the  idea behind what an object is. It is imperative that developers understand &#039;is...a&#039; and &#039;a...is...a&#039; relationships before attempting to utilize inheritance in their code. That aside, this was an good article Brandon.</description>
		<content:encoded><![CDATA[<p>This article was well written and the examples were crystal clear. I really don&#8217;t see what the fuss is about with some of the comments above. Levels of inheritance simply MUST follow a logical hierarchy or else you are setting yourself up for problems. The only (minor) gripe I have with this article is that the problem(s) outlined here are not exactly direct results of using multiple levels of inheritance but more results of bad design and failure to understand correctly the  idea behind what an object is. It is imperative that developers understand &#8216;is&#8230;a&#8217; and &#8216;a&#8230;is&#8230;a&#8217; relationships before attempting to utilize inheritance in their code. That aside, this was an good article Brandon.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joshua May</title>
		<link>http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/#comment-735</link>
		<dc:creator>Joshua May</dc:creator>
		<pubDate>Sat, 25 Jul 2009 08:00:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=472#comment-735</guid>
		<description>@Avi I wasn&#039;t saying there isn&#039;t more than one way to skin a cat, but it&#039;s often less elegant or harder to maintain than if it was just simply done right in the first place, and allowing people to make their own decisions.</description>
		<content:encoded><![CDATA[<p>@Avi I wasn&#8217;t saying there isn&#8217;t more than one way to skin a cat, but it&#8217;s often less elegant or harder to maintain than if it was just simply done right in the first place, and allowing people to make their own decisions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Avi Block</title>
		<link>http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/#comment-726</link>
		<dc:creator>Avi Block</dc:creator>
		<pubDate>Fri, 24 Jul 2009 22:25:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=472#comment-726</guid>
		<description>@Joshua Can you post an example of that which could not be solved by composition?</description>
		<content:encoded><![CDATA[<p>@Joshua Can you post an example of that which could not be solved by composition?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joshua May</title>
		<link>http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/#comment-721</link>
		<dc:creator>Joshua May</dc:creator>
		<pubDate>Fri, 24 Jul 2009 18:03:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=472#comment-721</guid>
		<description>I think inheritance (especially the argument/example you&#039;ve put up) is more about discipline than it is about making things final and private.

Just because you as the original developer don&#039;t see a reason why anything would need to be subclassed doesn&#039;t mean that someone else won&#039;t.

I&#039;ve run into a lot of headaches before where things were needlessly private (which you can work around, but it&#039;s not good practice) or things were marked final (which is even uglier to work around) because the original developer couldn&#039;t read my mind :)

For similar reasons people say PHP is good (beaucse it&#039;s so flexible, even if that flexibility comes at the cost of readability) OO is also good. It&#039;s a loaded gun aimed right at your foot..don&#039;t pull the trigger ;)</description>
		<content:encoded><![CDATA[<p>I think inheritance (especially the argument/example you&#8217;ve put up) is more about discipline than it is about making things final and private.</p>
<p>Just because you as the original developer don&#8217;t see a reason why anything would need to be subclassed doesn&#8217;t mean that someone else won&#8217;t.</p>
<p>I&#8217;ve run into a lot of headaches before where things were needlessly private (which you can work around, but it&#8217;s not good practice) or things were marked final (which is even uglier to work around) because the original developer couldn&#8217;t read my mind :)</p>
<p>For similar reasons people say PHP is good (beaucse it&#8217;s so flexible, even if that flexibility comes at the cost of readability) OO is also good. It&#8217;s a loaded gun aimed right at your foot..don&#8217;t pull the trigger ;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: derby</title>
		<link>http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/#comment-669</link>
		<dc:creator>derby</dc:creator>
		<pubDate>Mon, 20 Jul 2009 01:50:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=472#comment-669</guid>
		<description>Child classes overriding its parent method should not change the return type. I would imagine that if it is desired to have a similar method but with a different return type, then a new method should be created - since it would seem appropriate that any new functionality calling the method and expecting the different return type would be able to use the newly designated method.

public function getPropertiesArray() { //or something..
   return (array) $this-&gt;getProperties();
}</description>
		<content:encoded><![CDATA[<p>Child classes overriding its parent method should not change the return type. I would imagine that if it is desired to have a similar method but with a different return type, then a new method should be created &#8211; since it would seem appropriate that any new functionality calling the method and expecting the different return type would be able to use the newly designated method.</p>
<p>public function getPropertiesArray() { //or something..<br />
   return (array) $this-&gt;getProperties();<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jiri Fornous</title>
		<link>http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/#comment-654</link>
		<dc:creator>Jiri Fornous</dc:creator>
		<pubDate>Sat, 18 Jul 2009 14:30:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=472#comment-654</guid>
		<description>To Mathias:

It would be worth for many programmers to explain it in separate article - two points are well known (inheritance vs composition and interface vs implementation), the third is very important one...</description>
		<content:encoded><![CDATA[<p>To Mathias:</p>
<p>It would be worth for many programmers to explain it in separate article &#8211; two points are well known (inheritance vs composition and interface vs implementation), the third is very important one&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Giorgio Sironi</title>
		<link>http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/#comment-648</link>
		<dc:creator>Giorgio Sironi</dc:creator>
		<pubDate>Sat, 18 Jul 2009 10:00:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=472#comment-648</guid>
		<description>These are actually common patterns in the object-oriented world, maybe less known in php one. :)
http://www.google.it/search?q=favor+composition+over+inheritance
http://en.wikipedia.org/wiki/Liskov_substitution_principle</description>
		<content:encoded><![CDATA[<p>These are actually common patterns in the object-oriented world, maybe less known in php one. :)<br />
<a href="http://www.google.it/search?q=favor+composition+over+inheritance" rel="nofollow">http://www.google.it/search?q=favor+composition+over+inheritance</a><br />
<a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle" rel="nofollow">http://en.wikipedia.org/wiki/Liskov_substitution_principle</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: passerby</title>
		<link>http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/#comment-636</link>
		<dc:creator>passerby</dc:creator>
		<pubDate>Fri, 17 Jul 2009 18:56:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=472#comment-636</guid>
		<description>Good article but some people who still struggle with OOP get really annoyed with the Car-&gt;Honda-&gt;Civic and Plant-&gt;Tree-&gt;Oak examples.

You should establish your point with the fruits then back it up with an example a php programmer is likely to actually encounter in reality. Users and User groups or CMS articles or something.

Agree with first comment about misleading title.</description>
		<content:encoded><![CDATA[<p>Good article but some people who still struggle with OOP get really annoyed with the Car-&gt;Honda-&gt;Civic and Plant-&gt;Tree-&gt;Oak examples.</p>
<p>You should establish your point with the fruits then back it up with an example a php programmer is likely to actually encounter in reality. Users and User groups or CMS articles or something.</p>
<p>Agree with first comment about misleading title.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brandon Savage&#8217;s Blog: Where Multiple Inheritance Will Kill You &#124; Webs Developer</title>
		<link>http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/#comment-635</link>
		<dc:creator>Brandon Savage&#8217;s Blog: Where Multiple Inheritance Will Kill You &#124; Webs Developer</dc:creator>
		<pubDate>Fri, 17 Jul 2009 18:01:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=472#comment-635</guid>
		<description>[...] this new post to his blog Brandon Savage takes a look at multiple inheritance in PHP applications - specifically [...]</description>
		<content:encoded><![CDATA[<p>[...] this new post to his blog Brandon Savage takes a look at multiple inheritance in PHP applications &#8211; specifically [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gasper_k</title>
		<link>http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/#comment-633</link>
		<dc:creator>gasper_k</dc:creator>
		<pubDate>Fri, 17 Jul 2009 14:42:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=472#comment-633</guid>
		<description>I don&#039;t think your first example causes unpredictable behavior in any way. Your Execute function demands an instance of DoSomeWork, which includes instances of DoSomeMoreWork. So, the object passes this instanceof test. Then, a method runMyFunc() is executed on the object -- which means that the method in the latest child that defines the method gets invoked.

If the object is of DoSomeWork, then this executes DoSomeWork::runMyFunc(). If it&#039;s a DoSomeMoreWork object with that method defined, then &lt;em&gt;that&lt;/em&gt; method is called. The method could be defined in the BaseClass, as far as OOP is concerned. It&#039;s actually what inheritance is about; not knowing what is the &lt;em&gt;exact&lt;/em&gt; class, and still be able to execute a method. There is no unpredictable behavior here.

Apart from that, yes, a lot of inheritance approaches should be solved with composition.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t think your first example causes unpredictable behavior in any way. Your Execute function demands an instance of DoSomeWork, which includes instances of DoSomeMoreWork. So, the object passes this instanceof test. Then, a method runMyFunc() is executed on the object &#8212; which means that the method in the latest child that defines the method gets invoked.</p>
<p>If the object is of DoSomeWork, then this executes DoSomeWork::runMyFunc(). If it&#8217;s a DoSomeMoreWork object with that method defined, then <em>that</em> method is called. The method could be defined in the BaseClass, as far as OOP is concerned. It&#8217;s actually what inheritance is about; not knowing what is the <em>exact</em> class, and still be able to execute a method. There is no unpredictable behavior here.</p>
<p>Apart from that, yes, a lot of inheritance approaches should be solved with composition.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brandon Savage</title>
		<link>http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/#comment-632</link>
		<dc:creator>Brandon Savage</dc:creator>
		<pubDate>Fri, 17 Jul 2009 14:37:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=472#comment-632</guid>
		<description>Suter,

Point taken. I don&#039;t want to scare people away from inheritance. I want them to use it properly. Sadly, the examples I provided were dumbed-down versions of examples I&#039;ve seen in real life code, including some major frameworks and applications. Good architecture is essential.</description>
		<content:encoded><![CDATA[<p>Suter,</p>
<p>Point taken. I don&#8217;t want to scare people away from inheritance. I want them to use it properly. Sadly, the examples I provided were dumbed-down versions of examples I&#8217;ve seen in real life code, including some major frameworks and applications. Good architecture is essential.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Suter</title>
		<link>http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/#comment-631</link>
		<dc:creator>Suter</dc:creator>
		<pubDate>Fri, 17 Jul 2009 14:31:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=472#comment-631</guid>
		<description>Brandon,

Yep, you&#039;re right. I misread that class name. My bad.

But that doesn&#039;t change my point of not scaring people from inheritance. If used properly, it doesn&#039;t harm. And if you know what you&#039;re doing it won&#039;t kill anyone.</description>
		<content:encoded><![CDATA[<p>Brandon,</p>
<p>Yep, you&#8217;re right. I misread that class name. My bad.</p>
<p>But that doesn&#8217;t change my point of not scaring people from inheritance. If used properly, it doesn&#8217;t harm. And if you know what you&#8217;re doing it won&#8217;t kill anyone.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mathias</title>
		<link>http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/#comment-630</link>
		<dc:creator>Mathias</dc:creator>
		<pubDate>Fri, 17 Jul 2009 14:31:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=472#comment-630</guid>
		<description>The problems you described can be solved using well known principles in OOP theory:
1/ Favor composition over inheritance
2/ Program to interfaces, not implementations
3/ Encapsulates what varies
Other people can explain these better than I ever could, so just google it</description>
		<content:encoded><![CDATA[<p>The problems you described can be solved using well known principles in OOP theory:<br />
1/ Favor composition over inheritance<br />
2/ Program to interfaces, not implementations<br />
3/ Encapsulates what varies<br />
Other people can explain these better than I ever could, so just google it</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brandon Savage</title>
		<link>http://www.brandonsavage.net/where-multiple-inheritance-will-kill-you/#comment-629</link>
		<dc:creator>Brandon Savage</dc:creator>
		<pubDate>Fri, 17 Jul 2009 13:49:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=472#comment-629</guid>
		<description>Suter, I&#039;m sorry you missed the point. Cherries grow on trees, which makes them a variety of fruit called a tree fruit. And I think that in your case, you might want to apply the golden rule when commenting on other people&#039;s work.

Martin, it is what inheritance is made for - if you intend for that to happen. For example, if you direct the function to return a stdClass object and you override that telling it to return an array, you might get undesired functionality.

As for extending Cherry with FruitSalad, you&#039;d be amazed at how often I see that very mistake. Which is why I used that perhaps overly-simplified mistake - everyone can relate to the fact that a fruit salad is not made of a single fruit. On the other hand they might not be able to see that their database object isn&#039;t in the same family as their business logic. Hopefully they&#039;ll think about the example and give it some thought.</description>
		<content:encoded><![CDATA[<p>Suter, I&#8217;m sorry you missed the point. Cherries grow on trees, which makes them a variety of fruit called a tree fruit. And I think that in your case, you might want to apply the golden rule when commenting on other people&#8217;s work.</p>
<p>Martin, it is what inheritance is made for &#8211; if you intend for that to happen. For example, if you direct the function to return a stdClass object and you override that telling it to return an array, you might get undesired functionality.</p>
<p>As for extending Cherry with FruitSalad, you&#8217;d be amazed at how often I see that very mistake. Which is why I used that perhaps overly-simplified mistake &#8211; everyone can relate to the fact that a fruit salad is not made of a single fruit. On the other hand they might not be able to see that their database object isn&#8217;t in the same family as their business logic. Hopefully they&#8217;ll think about the example and give it some thought.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic (Feed is rejected)
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 3/11 queries in 0.046 seconds using disk: basic
Content Delivery Network via Amazon Web Services: S3: files.brandonsavage.net.s3.amazonaws.com

Served from: www.brandonsavage.net @ 2012-02-07 04:50:18 -->
