php - Change value of a variable of the parent class in the child class -


class {     protected $a;      // code }   class b extends {    // code } 

how can edit protected value of variable $a inside b class ?

i'm trying use parent::$a = "some value" doesn't work.

protected instance properties, not declared using static, can accessed in subclasses using $this :

class {     protected $a;      // code }   class b extends {    // code    public function edit($val) {        $this->$a = $val;        echo "a {$this->a}\n";    } } 

call:

$b = new b(); $b->edit('foo'); // foo 

refer manual, examples.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -