Answer the question
In order to leave comments, you need to log in
Data for views through the Application class, why is it bad?
//класс Приложения доступен во всех местах приложения
class Application {
public $data = array();//сюда сохраняем данные выборок, для отображения во вьюхах
...
}
//подключаем вьюхи
function include_view($fileName, $vars = array())
{
$app=Application::Instance();
...
ob_start();
include $fileName;
return ob_get_clean();
}
<h1><?=$app->data['page']['h1']?></h1>
<?=include_view('subView.php'); ?>
Answer the question
In order to leave comments, you need to log in
What people do not go to not use frameworks. But this is a philosophical question.
In order not to overwrite $app->data completely, make a method/setter on them. And it will be like $app->set('variable','value') or just $app->var = value;
Moreover, certain samples can be thrown into traits and used in the necessary controllers.
It is also possible to determine the need to load data through getters. For example, you only need to show some data on a part of the pages A. You write by type ...
if (!isset($this->data['A']){
$this->data['A'] = Foo::bar();
}
return $this->data['A'];
Because you have a) strong coupling - when each view must know what fields are in the app and which ones it needs, and b) with an increase in the number of handlers for this app, you simply won’t be able to determine which particular function / class writes incorrect data. Use the principles of OOP in practice, it helps a lot.
Yes, on a small project of 10 pages, the difference is not visible, but if it turns out something big and / or written by several people, there will be problems.
I don’t know why this is bad, although this approach is similar to using global variables, from here you can google why this is bad.
And on my own behalf, I can add that if you go the standard way and write phpDocs or whatever they are called in the view, then the variables will have auto -completion in the IDE.
And other developers understand what it is right from the View and do not need to go into the controller to see what the variable is.
Convenient, beautiful, one extra movement at the beginning and avoiding a thousand extra ones in the end.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question