Can Object Of A Parent Class Access Methods / Properties Of Child Class?
<?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!
?>
The problem:
The constructor and the destructor of MyClass aren't being executed as I'd expect. Would love to know why?