PHP and Classes: access to parent's public property within the parent class
Posted
by takpar
on Stack Overflow
See other posts from Stack Overflow
or by takpar
Published on 2010-03-13T17:06:44Z
Indexed on
2010/03/13
17:15 UTC
Read the original article
Hit count: 563
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()
© Stack Overflow or respective owner