Hunt for India's Best Academic Project Contest - 2015

Important Links To Visit: 

 

Click: List of Shortlisted Participants | Click: Round II Details (Important) 

 

Can Object Of A Parent Class Access Methods / Properties Of Child Class?

Discussion in 'Computer Science | IT | Networking' started by Kaustubh Katdare, May 29, 2014.

  1. Kaustubh Katdare

    Administrator

    I've began dedicating time to teach myself OOP and PHP and stumbled upon a very basic problem with inheritance. While the answers to the above question is already available online; I thought it'd help many new programmers understand the basics. So here we go.

    PHP:
    <?php

    class MyClass
    {
        public 
    $prop1 "This is my new property in MyClass <br/>";
       
        public function 
    __construct()
        {
            echo 
    "New Instance Of " __CLASS__ "has been created. <br/>";
           
        }
       
        public function 
    __destruct()
        {
            echo 
    "The class" __CLASS__ "has been destroyed. <br/>";
        }
       
        public function 
    __toString()
        {
            echo 
    "Now under toString method<br/>";
            return 
    $this-getProperty();
        }
       
        public function 
    setProperty($newVal)
        {
            
    $this->prop1 $newVal;
        }
       
        public function 
    getProperty()
        {
            return 
    $this->prop1 "<br/>";
        }
    }

    //Extending the MyClass

    class MyNewClass extends MyClass
    {
       
        
    //Let's build a new constructor here
        
    public function __construct()
        {
            echo 
    "I'm a new constructor inside" __CLASS__ "I'm inside extended MyClass. <br/>";
        }
       
        
    //..and one more destructor in here
       
        
    public function __destruct()
        {
            echo 
    "I'm a destructor of " __CLASS__ "inside the extended MyClass. <br/>";
        }
       
       
        public function 
    MyNewMethod()
        {
            echo 
    "Currently Under" __CLASS__ "ain't that cool?<br/>";
        }
    }

    $newobj = new MyNewClass;

    echo 
    $newobj->MyNewMethod();

    echo 
    "---------<br/>";

    $oldobj = new MyClass;

    echo 
    $oldobj->; // THIS IS WHERE THE PROBLEM LIES!

    ?>
    You see that I'm deliberately trying to echo object so that the toString() method is accessed from MyClass.

    The problem:

    The constructor and the destructor of MyClass aren't being executed as I'd expect. Would love to know why?
     
    • Like Like x 1
  2. it's not possible, I have not read anything like this before, you can't access methods of child class in parent class

    At least I never read this thing anywhere
     
    • Agree Agree x 1
  3. A father cannot claim to his sons assets but a son can.
     
  4. lol I was thinking of giving same example :D
     
  5. Kaustubh Katdare

    Administrator

    I'm reading about Abstract Classes. Do they offer any solution? By the way, is there any use case where the 'father' would need to claim son's assets? I know it'd make more sense to have those declared in the parent class - but can we think of an exception?
     
  6. Hmm serious doubts you asked.

    I dont think there is a way to claim son's assets since these assets are made for specialized behaviour.

    Abstract classes do provide solutions.
     
  7. To expand on abstract classes, They are like interfaces. They work as internal libraries so that child classes can use those attributes/variables etc easily.
     
  8. if you really want to use parent class methods in child class then you can just create new methods in child class for e.g I have method function hello() in parent then I can create another method in child function world() and call hello inside child using $this->hello(); makes sense?
     
    • Like Like x 2
: php

Share This Page