Jump to content

Questions on Inheritance


Darkranger85

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.