PHP and Classes: access to parent's public property within the parent class
- by takpar
Hi, here is what my code looks like
i have two forms:
class Form_1 extends Form_Abstract {
public $iId = 1;
}
class Form_2 extends Form_1 {
public $iId = 2;
}
i expect the code behave like this:
$oForm = new Form_2;
echo $oForm->getId(); // it returns '2'
echo $oForm->getParentId(); // i expect it returns '1'
here is my Form_Abstract class:
class Form_Abstract {
public $iId = 0;
public function getId() {
return $this->iId;
}
/**
this method will be called from a child instance
*/
public function getParentId() {
return parent::$iId;
}
}
but it throws a Fatal Error:
Fatal error: Cannot access parent:: when current class scope has no parent
please help me with the method getParentId()