Can Object Of A Parent Class Access Methods / Properties Of Child Class?
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.
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?
php
class MyClass
{
public $prop1 = "This is my new property in MyClass
";
public function __construct()
{
echo "New Instance Of " . __CLASS__ . "has been created.
";
}
public function __destruct()
{
echo "The class" . __CLASS__ . "has been destroyed.
";
}
public function __toString()
{
echo "Now under toString method
";
return $this-getProperty();
}
public function setProperty($newVal)
{
$this->prop1 = $newVal;
}
public function getProperty()
{
return $this->prop1 . "
";
}
}
//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.
";
}
//..and one more destructor in here
public function __destruct()
{
echo "I'm a destructor of " . __CLASS__ . "inside the extended MyClass.
";
}
public function MyNewMethod()
{
echo "Currently Under" . __CLASS__ . "ain't that cool?
";
}
}
$newobj = new MyNewClass;
echo $newobj->MyNewMethod();
echo "---------
";
$oldobj = new MyClass;
echo $oldobj->; // THIS IS WHERE THE PROBLEM LIES!
?>
The problem:
The constructor and the destructor of MyClass aren't being executed as I'd expect. Would love to know why?
Replies
-
Manish Goyalit'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 GoenkaA father cannot claim to his sons assets but a son can.
-
Manish Goyallol I was thinking of giving same example 😁
-
Kaustubh KatdareI'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 GoenkaHmm 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 GoenkaTo 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 Goyalif 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?
You are reading an archived discussion.
Related Posts
So, since we have a lot of startup talk going on around, all thanks to Founders Circuit, here's a question that came up in my mind. There are a lot...
Alcatel, the brand from the Chinese handset manufacturer - TCL, had come up with a new smartphone called "Alcatel One Touch Idol X+" and had officially launched it in China...
The inaugural Code Conference at Rancho Palos Verdes, Calif. is a Mecca for tech companies and technologists alike. Apart from the Skype Translator and the Apple-Beats deal another product was...
As you can see on CrazyEngineers, the breadcrumbs (or Navigation tabs) follow the responsive design. It means they adjust to the size of the screen. I want to know how...
Just came across this Coca-Cola's advertisement. I really love this idea.