Welcome Guest, Not a member yet? Register   Sign In
Helpers VS Config Files
#1

[eluser]BradEstey[/eluser]
Lets say I create a custom config file with the intention of storing global variables like directory paths, blocked IP arrays and so on. So lets say I store my Image directory location in a variable just incase I want to change it later and have it take effect globally. I would put the following in a custom config file:

Code:
$config['imgdir'] = 'img';

And autoload the custom config file...

Then whenever I want to call the image directory in a view or model or whatever, I have to write: $this->config->item('imgdir')

But that's pretty long... so If I create a helper file and create a function like this:

Code:
function imgdir() {
    return 'img';
}

Then autoload that helper, all I have to write to call the image directory is imgdir() .... which is a whole lot shorter.

I tried creating functions to call from my custom config file but they don't work.

Code:
function imgdir() {
    return $this->config->item('imgdir');
}

Is there any downside to using a helper to store these variables over using a custom config file? Does calling a function take up more resources?
#2

[eluser]danmontgomery[/eluser]
You can't access $this outside of a class. To access the CI object you need to use get_instance():

Code:
function imgdir() {
    $CI =& get_instance();
    return $CI->config->item('imgdir');
}

You can also use the config_item() function (globally):

Code:
echo config_item('imgdir');

No matter how you access it you are making a function call, resources are not really an issue.
#3

[eluser]BradEstey[/eluser]
is the "=&" needed in PHP5 or can it just be "="?
#4

[eluser]danmontgomery[/eluser]
The ampersand tells PHP to assign a reference, rather than create a copy. It's not "needed", but you should not remove it.

http://php.net/manual/en/language.references.php
#5

[eluser]Aken[/eluser]
I say if you're going to write the helper functions, you might as well bypass the config variables all together. Create a helper file, autoload it if you want/need to, then define all your variables there.
Code:
function imgdir() {
    return 'img';
}
#6

[eluser]BradEstey[/eluser]
Thanks Aken, that was my thought too. But, in the interest of keeping logic separate from data, I've opted to keep my config file.




Theme © iAndrew 2016 - Forum software by © MyBB