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

    Engineering Discipline:
    Electrical
    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
    #1
  2. Manish Goyal

    Engineering Discipline:
    Computer Science
    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
    #2
  3. Nayan Goenka

    Engineering Discipline:
    IT
    A father cannot claim to his sons assets but a son can.
     
    #3
  4. Manish Goyal

    Engineering Discipline:
    Computer Science
    lol I was thinking of giving same example :D
     
    #4
  5. Kaustubh Katdare

    Administrator

    Engineering Discipline:
    Electrical
    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?
     
    #5
  6. Nayan Goenka

    Engineering Discipline:
    IT
    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.
     
    #6
  7. Nayan Goenka

    Engineering Discipline:
    IT
    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.
     
    #7
  8. Manish Goyal

    Engineering Discipline:
    Computer Science
    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
    #8
: php

Share This Page