Welcome Guest, Not a member yet? Register   Sign In
constructor - why, where, when??
#9

[eluser]jedd[/eluser]
[quote author="boony" date="1234679784"]OK,
Thanks, that seems to be a bit clearer. I'm guessing, to be on the safe side, that it may be advisable to use a constuctor method in all controllers and models that I create, this way I can safely add specific methods to the subclass (or not as the case may be) and be sure that the method will work.
[/quote]

It's safe to say that, for the moment, all the classes you make will include a constructor (even if its empty other than the parent:: line). This assumption is based on the idea that you are most likely to be creating Controller and Model classes only (views don't the issue), at least in the short term.

Quote:From what pistolPete posted I get the idea that all library-helper classes, as well as the model, should be loaded into the constructor methods and not into the individual methods that I create within the controller.

I don't think that's what he was saying. If all the methods - think of them as functions that are grouped together to work on the object's data - have similar requirements (and really, they should if you design things right) then it's *probably* sensible to have the libraries and helpers called in the constructor (because all this object's methods will make use of them).

BUT if one of your methods in a class does something special that needs a library or helper that none of the other methods do, then it's good practice to make that lib/help load call in the method itself. Better performance for starters.

Note that I'm about 27 minutes ahead of you with this stuff .. so keep reading through the user_guide, particularly going over the M, V, and C pages - as well as the CI wiki. Read everything carefully, as very important ideas are often slipped in where you don't expect them. Wink

Quote:From this I take it that any library class eg $this->load->library('table') will be available globally if placed in the constructor but will only be available to when a specific function is called if it is placed within that method. Is this correct??

Globally is a very strong word. But no. Not global.

They'll be available within the class, yes. To all methods within that class, to really spell it out.

Quote:Finally, what about the call to the view? Is that also always called in the constructor?? Or only called in the method the builds the view?

No .. calls to views are typically done within your methods within the Controller object. You *might* want to call a frequently used common view to construct some HTML from your constructor - but for now I'd suggest you don't.

Quote:Hope I'm not asking the bleeding obvious, but these things seem to be very important and it I would like to make sure that I get the syntax correct to save future head banging (a thing I seem to do a lot of :-)

Future head-banging is guaranteed. Don't fight it.


Messages In This Thread
constructor - why, where, when?? - by El Forum - 02-14-2009, 05:27 PM
constructor - why, where, when?? - by El Forum - 02-14-2009, 06:13 PM
constructor - why, where, when?? - by El Forum - 02-14-2009, 06:13 PM
constructor - why, where, when?? - by El Forum - 02-14-2009, 06:14 PM
constructor - why, where, when?? - by El Forum - 02-14-2009, 06:36 PM
constructor - why, where, when?? - by El Forum - 02-14-2009, 06:40 PM
constructor - why, where, when?? - by El Forum - 02-14-2009, 07:25 PM
constructor - why, where, when?? - by El Forum - 03-01-2009, 10:05 AM
constructor - why, where, when?? - by El Forum - 03-01-2009, 10:34 AM
constructor - why, where, when?? - by El Forum - 03-01-2009, 10:56 AM



Theme © iAndrew 2016 - Forum software by © MyBB