oop - How do I call the parent classes constructor with its original parameters in PHP -


i have code:

class {   var $arr = array();    function __construct($para) {     echo 'not called';   } }  class b extends {   function __construct() {     $arr[] = 'new item';   } } 

and b has own constructor construct($para) of never gets called.

now call parent::__construct($para) class b need aware of parameters class needs.

i prefer this:

class {   var $arr = array();    function __construct($para) {     echo 'not called';   } }  class b extends {   function __construct() {     parent::__construct(); // parameters class b created.      // additional actions not need direct access parameters     $arr[] = 'new item';   } } 

would work?

i don't fact, classes extend class need define new constructor, once class changes parameters, want them call constructor of class when class b not overwrite own __construct() method.

one solution not override parent constructor in first place. instead, define separate (initially-empty) init() method parent constructor calls automatically. method overwritten in child in order perform processing.

class {     public function __construct($para) {         // parent processing using $para values          // ..and run child initialization         $this->init();     }     protected function init() {     } }  class b extends {     protected function init() {         // additional actions not need direct access parameters     } } 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -