Darkranger85 Posted January 24, 2012 Share Posted January 24, 2012 Hey again, I hope you guys don't get annoyed with my noob questions. I was reading the guide here on the site about OOP, and on the section related to inheritance it makes a "Dog" class, after giving it an attribute and a method it then creates the "Animal" class using "extends" and expands on the methods. Then after that here is the paragraph I'm looking at now. "Class Animal is unaware of the fact that it is being extended; there are no references to Dog whatsoever. Say Animal extended another class called LifeForm, and I instantiated Animal, only methods and properties of Animal and LifeForm would be included in the object." To me, this sounds like if I were to create this "LifeForm" class by further extending the "Animal" class, I would not be able to access any of the attributes and methods that were in "Dog," only the ones that were in Animal and any that I put in LifeForm itself. But, I was playing with the code and I can call an attribute from Dog in LifeForm. So, am I reading the post wrong or am I coding wrong? <?php //Class Dog// class Dog{ public $hungry = 'Hell yeah!'; public $test = 'Affirmative!'; function eat($food){ $this->hungry = 'not so much.'; } } //Class Animal// class Animal extends Dog{ function eat($food){ if($food === 'cookie'){ $this->hungry = 'not so much'; }else{ echo 'Barf! I only like cookies!'; } } } //Class LifeForm// class LifeForm extends Animal{ function eat($food){ } } //Program Variables// $dog = new Dog; $LifeForm = new LifeForm; //Program Code// echo $LifeForm->test; ?> My assumption was that I am able to call that attribute because of the 'public' in front of it, but all the attributes in the examples are also public. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/255657-questions-on-inheritance/ Share on other sites More sharing options...
Proletarian Posted January 24, 2012 Share Posted January 24, 2012 You're extending LifeForm from Animal, which is extended from Dog. You have your base class wrong. Basically, Dog is your LifeForm's ancestor, which means LifeForm can call those inherited properties from Dog. Animal should be your base class. Dog and LifeForm should extend animal. Now, Dod and LifeForm share an ancestor, but do not share each other's unique class properties. Quote Link to comment https://forums.phpfreaks.com/topic/255657-questions-on-inheritance/#findComment-1310554 Share on other sites More sharing options...
Darkranger85 Posted January 24, 2012 Author Share Posted January 24, 2012 Ooohh ok. So this "family tree" is extending outward from the base and not in a straight line downward? I have to say that makes things WAY clearer. Again, I apologize for being a noob lol, and thank you for helping! Quote Link to comment https://forums.phpfreaks.com/topic/255657-questions-on-inheritance/#findComment-1310555 Share on other sites More sharing options...
Proletarian Posted January 24, 2012 Share Posted January 24, 2012 The point of inheritance is to specialize a base class. You can have multiple layers of inheritance, but it's generally not a good idea to extend more than 1 or 2 levels down. Quote Link to comment https://forums.phpfreaks.com/topic/255657-questions-on-inheritance/#findComment-1310558 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.