Passing objects to views

I have a general question. If I have an object, a model called user let’s say, which has the typical user info, username, user_id, last_access, email_address…etc. If I only needed the username and email_address for a given view, would it be better to create an array in the controller i.e.


$UserInfo = ('name'=>user->username, 'address'=>user->email_address) 

and pass that to the view instead of passing a whole object, or number of objects. In terms of performance is that something one should aim to do whenever possible? Is passing whole objects to views common practice or generally avoided?

I don’t see the difference. You have to instantiate the object anyway, plus the DB retrieval is going to be the slowest part. I don’t see any advantage in creating an array and passing that instead. If you want to implement future changes you actually LOSE bigtime because not only do you have to update your view, you also have to redefine your array.

Don’t prematurely optimize. If your site is done and you run into slowness on this particular view you may want to optimize it later. For now, start with full AR objects and don’t worry too early.

Cool, thanks for the advice.