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

Kaustubh Katdare

Kaustubh Katdare

@thebigk Oct 22, 2024
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?

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep May 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
  • Nayan Goenka

    Nayan Goenka

    @nayan-Dhpt4N May 29, 2014

    A father cannot claim to his sons assets but a son can.
  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep May 29, 2014

    lol I was thinking of giving same example 😁
  • Kaustubh Katdare

    Kaustubh Katdare

    @thebigk May 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?
  • Nayan Goenka

    Nayan Goenka

    @nayan-Dhpt4N May 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.
  • Nayan Goenka

    Nayan Goenka

    @nayan-Dhpt4N May 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.
  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep May 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?