Note that there are some explanatory texts on larger screens.

plurals
  1. POOOP: Problem accessing parent's property from the extended child
    primarykey
    data
    text
    <p>I try to access the parent's property from its extended child similar to the concept below,</p> <pre><code>class Base { protected static $me; protected static $var_parent_1; protected static $var_parent_2; public function __construct ($var_parent_1 = null) { $this-&gt;var_parent_1 = $var_parent_1; $this-&gt;me = 'the base'; } public function who() { echo $this-&gt;me; } public function parent_1() { echo $this-&gt;var_parent_1; } } class Child extends Base { protected static $me; protected static $var_child_1; protected static $var_child_2; public function __construct ($var_child_1 = null) { parent::__construct(); $this-&gt;var_child_1 = $var_child_1; $this-&gt;me = 'the child extends '.parent::$me; } // until PHP 5.3, will need to redeclare this public function who() { echo $this-&gt;me; } public function child_1() { echo $this-&gt;var_child_1; } } $objA = new Base($var_parent_1 = 'parent var 1'); $objA-&gt;parent_1(); // "parent var 1" $objA-&gt;who(); // "the base" $objB = new Child($var_child_1 = 'child var 1'); $objB-&gt;child_1(); // "child var 1" $objB-&gt;who(); // should get "the child extends the base" </code></pre> <p>But I get <strong>"the child extends"</strong> instead of <strong>"the child extends the base"</strong> if I use <code>$this-&gt;</code> </p> <p>It seems OK if I change all <code>$this-&gt;</code> to <code>self::</code></p> <p>Why?</p> <p>Is that the only proper way to access the parent's property which is to change all <code>$this-&gt;</code> to <code>self::</code>?</p> <p><strong>EDIT:</strong></p> <p>I removed all <code>static</code> keywords,</p> <pre><code>class Base { protected $me; protected $var_parent_1; protected $var_parent_2; public function __construct ($var_parent_1 = null) { $this-&gt;var_parent_1 = $var_parent_1; $this-&gt;me = 'the base'; } public function who() { echo $this-&gt;me; } public function parent_1() { echo $this-&gt;var_parent_1; } } class Child extends Base { protected $me; protected $var_child_1; protected $var_child_2; public function __construct ($var_child_1 = null) { parent::__construct(); $this-&gt;var_child_1 = $var_child_1; $this-&gt;me = 'the child extends '.parent::$me; } // until PHP 5.3, will need to redeclare this public function who() { echo $this-&gt;me; } public function child_1() { echo $this-&gt;var_child_1; } } $objA = new Base($var_parent_1 = 'parent var 1'); //$objA-&gt;parent_1(); // "parent var 1" //$objA-&gt;who(); // "the base" $objB = new Child($var_child_1 = 'child var 1'); $objB-&gt;child_1(); // "child var 1" $objB-&gt;who(); // "the child extends the base" </code></pre> <p>Then I get this error <code>Fatal error: Access to undeclared static property: Base::$me in C:\wamp\www\test\2011\php\inheritence.php on line 109</code> which refers to this line,</p> <pre><code>$this-&gt;me = 'the child extends '.parent::$me; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload