Hello! There is a small MVC system. alt text This is not her complete scheme, but enough.

For example, in the controller controll_page.php , I do the necessary operations, assigning some variables to the values ​​that were returned to me by the model.

And pass them on to the template. Nd:

$subject = index::list_subject(); // Массив с предметами open::view("index/header"); open::view_o("index/index",$array); open::view_o("index/sidebar",$subject); open::view("index/footer"); 

there was a problem that, for example, in almost all controllers, you need to pass the $ subject variable. Each time to call the model will be costly for resources Since over time will be expanded. Sorry for the photo. How to reorganize the structure?

  • not quite clear why "in almost all controllers"? Do you use several controllers at once to form one page? it shouldn't be like that - psychomc
  • No, 1 staryna = 1 controller. - alex159
  • A view is a separate level in which data must be sent once: class JajaController {public function action () {$ data = Model :: fetchData (); echo View :: render ('index', $ data); }} What happens in the render method is that the controller doesn't care. And it is there that one should understand where the cap is, where the footer is, where the main body is. In a pinch, you can cache the data on the model side. - etki
  • Ie you first need to collect everything in one class? - alex159
  • @ alex159, in one module . It is implemented through a class or something else - that is another question. - etki

1 answer 1

For this there is a pattern / approach - I don’t know how to correctly name: Lazy Loading . Its meaning is that an object performs its most resource-intensive logic only when it is requested for the first time. For example, in your case on the line:

  $subject = index::list_subject(); // Массив с предметами 

There should be no queries in the database, and in $ subject, instead of an array, the object to be iterated should be returned. Then at the first iteration in the current iterator method, you need to make this very query in the database to select the list.

 protected $_list = []; protected $_initalized = false; public function current() { if (!$this->_initalized){ $this->_list = $this->_loadSomeListFromDB(); $this->_initalized = true; } return $this->_list[$this->position]; } 

But the lazy approach spawns additional logic where it is used. To avoid this, a Lazy container is introduced. For example, it is called Restore, or ServiceManager - everything Lazy is encapsulated in a container. For example, this is how we initialize the connection to the database in ServiceManager:

 ServiceManager::addServiceCallback('db', function() use ($dbUser, $dbPassword, $dbHost){ return new PDO('mysql:host='.$dbHost.';dbname=myproject', $dbUser, $dbPassword); }) 

and then when the code is called for the first time

 $rows = ServiceManager::get('db')->query('SELECT * from FOO'); 

The db service is initialized, and all subsequent calls to the ServiceManager::get('db') will be taken from the already initialized db service. An example of a good Lazy container implementation: Zend 2 ServiceManager

PS What does not necessarily singleton - mark, because they are not very fashionable now.