Welcome Guest, Not a member yet? Register   Sign In
My Class Libraries Living Between Methods Calls in Controller
#11

[eluser]CodeIgniterNewbie[/eluser]
Speed? Well, instead of having objects, why not just throw everything in session? If I need any methods, I can just put them in the controller. Sure, the controller may have a bunch of unrelated methods, but it'll still work. How is the object approach better than this?

(BTW, I did a quick test on serialize and unserialze. They work as advertised. So, if I need to persist the object, I just serialize it, save it to session (?), then unserialize it after the next page load?)
#12

[eluser]CodeIgniterNewbie[/eluser]
So, having the model aware of my class library and instantiating objects of it does NOT break MVC, right?
#13

[eluser]m4rw3r[/eluser]
I was referring to auto loading of the classes (ex. your controller always loads those objects). You can always make it faster, but you'll lose maintainability and your code will be more coupled with the rest, making it harder to reuse code.

About (un)serialize, just serialize the object to persist, save it anywhere you can load it from in the next request (session can be a good choice, the objects will be user (or at least session) persistent, but users can tamper with the data and you have a storage limit. Plus, the data is sent over the connection every request, so anyone can read it unless you use SSL), then you check if the object exists and if it does, load it, otherwise you have to construct a new one.
#14

[eluser]CodeIgniterNewbie[/eluser]
I guess I wasn't clear.

Let's say I auto load my classes so that the controller always has access those objects. In order to populate the objects, I go to the model, fetch the information, return it to the controller, and put the information in the object (or maybe the model returns it as an object already).

Is there any benefit to that versus just putting the information straight into session? If I need to do any processing, I can just methods in the controller (versus using ones available in the object).
#15

[eluser]m4rw3r[/eluser]
I would put the instantiation and loading of data in the model, so the controller does not need to handle the data.

The benefit depends on the type of data, amount and what you like. So maybe session variables (with methods in the controller) is better for you, or maybe not.
#16

[eluser]mattskelton[/eluser]
@CodeIgniterNewbie,

You are 100% right. Create/populate your business objects in your Model. Return those objects to the Controller. Serialize them if you need to persist to the next page/whole session.

Matt




Theme © iAndrew 2016 - Forum software by © MyBB