properties - PHP OOP how to access class parent property Scope Resolution Operator (::)? -


how can access property var in class otherclass inside class method myfunc (parent::myfunc();)

<?php  class myclass {     public $var = 'a';     protected function myfunc()     {         echo "myclass::myfunc()\n";     } }  class otherclass extends myclass {     public $var = 'b';     // override parent's definition     public function myfunc()     {         // still call parent function         parent::myfunc();          echo "otherclass::myfunc()\n";     } }  $class = new otherclass(); $class->myfunc(); 

you cannot, because there's no separate variable. otherclass extends myclass therefore otherclass contains myclass features + additional stuff otherclass whilte keeping access parent's methods (thru parent::) makes perfect sense (i.e allows chaining) having more 1 variable of same name cause massive headache w/o bringing benefits.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

Kivy: Swiping (Carousel & ScreenManager) -

jdbc - Not able to establish database connection in eclipse -