Question:
I have a base class " class Controller_BaseC extends Controller_Template ", which has its own view "' baseV '"
and class " class Controller_IndexC extends Controller_BaseC " without view.
In the "IndexC" class, I defined the scripts "$ this-> template-> scripts = array ('media / Js / loadscript.js');"
But in the base, I also defined "$ this-> template-> scripts = array ('media / Js / statr.js');"

How can I write so that the “IndexC” scripts do not overwrite the “BaseC” scripts, but add to them?

  • one
    $a = array(); $a[] = 'script.1.js'; $a[] = 'script.2.js'; - E_p
  • I apologize, did not catch your thoughts, in two classes I fill the arrays, now I need to combine them into a view, ie "$ this-> template-> scripts =" - Konstantin78
  • I filled the answer to the correct. - E_p

1 answer 1

To add a value to PCP, use the following syntax:

 <?php $a = []; $a[] = 'a'; $a[] = 'b'; // $a = array ('a', 'b') 

If the question is how to choose values ​​from the father class in the inherited class:

 <?php class A { public function getTemplates() { return $this->_templates; } public function initTemplates() { $this->_initTemplates(); } protected $_templates = ['1', '2', '3']; } class B extends A { protected function _initTemplates() { $this->_templates = array_merge( $this->_templates, $this->_getClassTemplates() ); } protected function _getClassTemplates() { return ['a', 'b', 'c']; } } class C extends B { protected function _getClassTemplates() { return array_merge( parent::_getClassTemplates(), ['d', 'e', 'f'] ); } } $a = new C(); $a->initTemplates(); var_dump($a->getTemplates()); die(); 

Not beautiful, but massive will work.

PS: It seems to me that you made an archiectural error. Without knowing the code it is difficult to say where.

  • You are not mistaken in the first comment. In both controllers, you need to connect scripts in the following way: " $ this-> template-> scripts = array (); $ this-> template-> scripts [] = 'media / Js / watch.js'; .... $ this-> template-> scripts [] = 'media / Js / analytics.js'; "well, etc. and everything will unite - Konstantin78