I
I
Ivan2015-04-01 23:18:18
Yii
Ivan, 2015-04-01 23:18:18

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();	
}

Further somewhere in the view
<h1><?=$app->data['page']['h1']?></h1>
<?=include_view('subView.php'); ?>

Thus, neither in the model nor in the controller does it need to list the variables passed to the view, the advantage is that when finalizing the project, you do not need to make sure that the variables are necessarily written in the controller and model. Minimization of edits.
What's bad? In addition, you can accidentally overwrite something in $app-> data in any module. Why are variables hardcoded in popular frameworks, and sometimes when adding a field to a table, you have to register a variable and commit another dozen files, and then write crutches and fence gardens in order to display in the layout (parent view) a variable that was created in the module that outputs mapping to a local widget view.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Ramallah, 2015-04-02
@ramallah

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'];

H
He11ion, 2015-04-02
@He11ion

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.

N
Nikita, 2015-08-19
@bitver

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 question

Ask a Question

731 491 924 answers to any question