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

    Kaustubh Katdare

    Kaustubh Katdare

    @thebigk
    Updated: Oct 22, 2024
    Views: 1.3K
    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

    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?
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Manish Goyal

    MemberMay 29, 2014

    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
    Are you sure? This action cannot be undone.
    Cancel
  • Nayan Goenka

    MemberMay 29, 2014

    A father cannot claim to his sons assets but a son can.
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberMay 29, 2014

    lol I was thinking of giving same example 😁
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorMay 29, 2014

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • Nayan Goenka

    MemberMay 29, 2014

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Nayan Goenka

    MemberMay 29, 2014

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberMay 29, 2014

    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?
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register