php - Constructor argument not inheriting down to children? -
i new php , in particular oop. have piece of test code thought return:
what result?8 result?8
but, instead getting:
what result?5 result?8
the arguments passed instance of class seem not getting assigned x. have tried echo $second->x
returns nothing.
have got code wrong, misunderstanding inheritance or misunderstanding constructors
?
here code:
<?php class first{ public function __construct($x){ $this->x = $x; echo "what result?"; } } class second extends first{ public function calculation(){ $z=5; return $x+$z."<br />"; } } class third extends first{ public function calculation(){ $z=5; $x=3; $y=$x+$z; return $y."<br/>"; } } $second = new second('3'); echo $second->calculation(); $third = new third('3'); echo $third->calculation(); ?>
just little updation on within second
class
class second extends test{ public function calculation(){ $z=5; return $this->x+$z."<br />";//$x should $this->x } }
output:
what result?8 result?8
Comments
Post a Comment