H
H
HellWalk2017-08-08 22:04:41
Yii
HellWalk, 2017-08-08 22:04:41

Is using compact to pass models to the view a good idea or vice versa?

Now, at work, I delve into one large project, where there is a lot where the method of transferring models to a view via compact is used, which makes arrays from objects:

return $this->render('main-view', compact('comInfo', 'SearchingForm',
'revCompany', 'modelComment', 'insuranceType'));

This approach is not entirely clear to me - for example, the author of the code transfers both arrays (through which he displays data) and objects (in order to assemble ActiveForm through them) to one page - I have a logical question - why? Isn't it easier to pass objects and continue to work with them for both output and input of information.
But I'm a green junior who has been working with Yii2 for a week, maybe I don't understand something.
Update
I realized my mistake - I thought that compact converts objects into arrays, but objects remain objects.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly Khomenko, 2017-08-08
@HellWalk

return $this->render('main-view', ['comInfo' => $comInfo, 'SearchingForm' => $SearchingForm,
'revCompany' => $revCompany, 'modelComment' => $modelComment, 'insuranceType' => $insuranceType]);

Something has changed? For you, the line has become longer, but nothing has changed for PHP. And about the number of parameters passed during the renderer, alas, this already depends on the developer and his programming style.

D
Decadal, 2017-08-08
@Decadal

Your question itself sounds one at a time, and the description of the question leads to another discussion.
About compact: Mark Safronov's book recommends this transfer method. It rarely occurs in practice. As far as I remember, PhpStorm does not pick up such a thing and then swears - perhaps the only negative.
And about this - well, yes, you are right, it is strange to pass both arrays and objects that can be reduced to these arrays. But maybe this is how you understood the code, but in fact the idea was deeper. Without the code itself, you can’t tell if it makes sense to do this or not

M
Maxim Timofeev, 2017-08-09
@webinar

passing models to view via compact

It's a matter of taste, I don't particularly like it, but a matter of habit. And through compact the string is definitely shorter. So that. it's probably the norm. But what does this have to do with objects and arrays? compact has no effect on this. This is a form of writing, roughly speaking. There can be objects and arrays and strings and whatever.
Objects consume more resources than arrays. So when developing, it is better to use objects, and at the optimization stage, where possible, replace the part with arrays. But it's still not clear what side this is connected with compact

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question