CodeIgniter Forums

Full Version: global objects
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Dennis_gull[/eluser]
Is there a best practice for global objects in CI? I currently keep protected variables in MY_controller but I won't be able to access them in my model.

I know two ways to solve this, I can either use "global $user" or put the user in a singleton however I think using a singleton is a little overkill so if there is no other way to pass data to each model automatically I think I will go with the global alternative.

By the way, can't I extend model and pass over the user from the controller in some way?

El Forum

[eluser]jgetner[/eluser]
there is many ways to grab your class properties without having to use globals best suggestion and overall best practice of php is to just build a simple method to call that classes properties.

i would suggest it being static so you can use that method globally if you remember to keep the class in scope.

El Forum

[eluser]Dennis_gull[/eluser]
I totally forgot about static variables, thanks for the help.