php - OOP-Inheritance: $this in subclass Y (which inherits from X) is pointing to X when method from Y called from X -


i have huge project , in point of extending want $this in subclass y (which inherits x) pointing x when method y called x :) can't interfere in class structure , want nicely without "helpers", additional constructor parameters "controller path" or that.

so problem is: when instantiate controllergallery calls constructor calls parent contructor (from controllerresource parent of controllergallery). in parent constructor (controllerresource), variable $this pointing controllergallery, not controllerresource.

i know when instantiate class, there 1 object created (only "controllergallery", not: "controllergallery , it's parent controllerresource"), , that's problem. question is: how achieve result shown below? suggestions?

<?php  abstract class controller {     function getparentcontroller()     {         return null;     }      function getcontroller(){         return $this;     } }  class controllerresource extends controller {     protected $controller_path = 'resource';     protected $scripts;      function __construct(){         $this->addscript($this->controller_path.directory_separator.'general_resource_support_script.js');     }      function addscript($name){         $this->scripts[] = $name;     }      function getparentcontroller()     {         return parent::getcontroller();     }      function getscriptpaths(){         return $this->scripts;     } }  class controllerproduct extends controllerresource {     protected $controller_path = 'product';     function __construct(){         parent::__construct();         $this->addscript($this->controller_path.directory_separator.'product_support_scripts.js');     } }  class controllergallery extends controllerresource {     protected $controller_path = 'gallery';     function __construct(){         parent::__construct();         $this->addscript($this->controller_path.directory_separator.'gallery_scripts.js');     } }    $controllerproduct = new controllerproduct(); $controllergallery = new controllergallery();  echo('<pre>'); print_r($controllerproduct->getscriptpaths()); print_r($controllergallery->getscriptpaths()); echo('</pre>');  echo(' <pre> <b>should be:</b> array (     [0] => <b>resource</b>\general_resource_support_script.js     [1] => product\product_support_scripts.js ) array (     [0] => <b>resource</b>\general_resource_support_script.js     [1] => gallery\gallery_scripts.js ) </pre> ');   ?> 

and result:

array (     [0] => product\general_resource_support_script.js     [1] => product\product_support_scripts.js ) array (     [0] => gallery\general_resource_support_script.js     [1] => gallery\gallery_scripts.js ) 

but should be:

array (     [0] => resource\general_resource_support_script.js     [1] => product\product_support_scripts.js ) array (     [0] => resource\general_resource_support_script.js     [1] => gallery\gallery_scripts.js ) 

protected $controller_path declared in both, controllerresource , controllerproduct classes. in php can redeclare public , protected method, since controllerproduct extends controllerresource value of variable value in derived class. if change visibility of $controller_path property private belong class , not overridden.

php visibility


Comments