I have this two classes:
<?php
class Test
{
protected $x = 0;
public function setX($x)
{
$this->x = $x;
}
public function getX()
{
echo $this->x;
}
}
class TestEx extends Test
{
//parent::$this->x = 7; it gives this error: Parse error: syntax error, unexpected '$x' (T_VARIABLE), expecting function (T_FUNCTION) in test.php
public function getX()
{
echo parent::$this->x;
}
}
$call_textex = new TestEx();
$call_textex->getX();
?>
Now, I would to set $x property of base class from the inherited class. How to achieve that in PHP5?
Put your assignment in the constructor, and just use
$this:See it here in action: http://codepad.org/bbrGXFWP