Parent class: P
Class p{ Private $name = ""; function __construct () { $this->name= "Hello"; } Public Function __set ($name, $value) { $this, $name = $value; } Public Function ShowName () { echo $this->name; }}
Sub-class: C
Class C extends p{ function __construct () { parent::__construct (); What do you do if you want to change the value of P-class $name (hello) here? } }
$c =new C;
$c->showname;
Then output: Hello
How to do it?
Reply to discussion (solution)
Class p{ Private $name = ""; function __construct () { $this->name= "Hello"; } Public Function __set ($name, $value) { $this, $name = $value; } Public Function ShowName () { echo $this->name; }} Class C extends p{ function __construct () { parent::__construct (); $this->name = ' Hello '; } } $c =new C; $c->showname ();p rint_r ($c);
How are you doing
C Object
(
[Name:p:private] = Hello
)
Class p{ Private $name = ""; function __construct () { $this->name= "Hello"; } Public Function __set ($name, $value) { $this, $name = $value; } Public Function ShowName () { echo $this->name; }} Class C extends p{ function __construct () { parent::__construct (); What do you do if you want to change the value of P-class $name (hello) here? $this->name = ' Hello '; }} $obj = new C (); $obj->showname ();