echo $this->a;
echoes value of class property a
. This property is not defined, because property a
of class Test
is private and therefore is not available in class Test2
. So, property a
is created in class Test2
.
echo $this->a = 20; // wh
does the next: assigns 20 to a
property (which was created on the previous line) and echoes result of assignment which is 20
.
The solution:
class Test
{
// protected property is avalilable in child classes
protected $a = 10;
public $b ='abc';
}
class Test2 extends Test
{
function __construct()
{
echo $this->a;
$this->a = 20;
echo $this->a;
}
}
$test3 = new Test2(); // outputs 10 20