Welcome Guest, Not a member yet? Register   Sign In
Can't access global variable from a helper?
#1

[eluser]stormbytes[/eluser]
I'm writing a helper that generates a navigation link list. I'm trying to access a global var (that's been auto-loaded through a class) within my function. I try to declare "global $var" but still can't get it. Tried echo'ing the $var in a view and it resolves just fine.

The $var actually originates as a $config['param'] and is loaded by my (separate) library. I thought to try to access the config param directly by using CI=& get_instance() followed by $CI->config-item('param'). But that can't seem to access the variable either!?

What am I missing?
#2

[eluser]WanWizard[/eluser]
What is your definition of a 'global' var? Where did you define it, and how?
#3

[eluser]Phil Sturgeon[/eluser]
[quote author="stormbytes" date="1288101248"]I'm writing a helper that generates a navigation link list. I'm trying to access a global var (that's been auto-loaded through a class) within my function. I try to declare "global $var" but still can't get it. Tried echo'ing the $var in a view and it resolves just fine.

The $var actually originates as a $config['param'] and is loaded by my (separate) library. I thought to try to access the config param directly by using CI=& get_instance() followed by $CI->config-item('param'). But that can't seem to access the variable either!?

What am I missing?[/quote]

Dude this is the 4th or 5th post by you that made no real sense...

Code:
global $var;

That has nothing to do with the $this super-global which is basically an instance of CI_Base then extended down through Controller and various chunks are assigned to Model and all available in Views.

You are trying to do some really weird, badly coupled stuff and explaining it really badly.

Explain it a little more and maybe we can help you find the right way, but right now we are just confused...
#4

[eluser]stormbytes[/eluser]
Sorry about that Phil -

I wrote that post at 4am. I guess I was pretty tired and perhaps even somewhat incoherent.

I'm writing a helper function that will output individual list-elements (<li><a>nav-link-item</a></li>) to a navigation menu. I'm using the URI class 'uri_string()' function to determine the 'current-page' when deciding if a given list-element should output as a link or static text. If the 'current-page' is say, 'about', the 'about' list-element will output as static text since no link is needed (the user is at that page).

The site's root URL can either be "example.com/default-controller", in which case uri_string() will return '/default-controller', or it can be "example.com" in which case uri_string() will return "". To trap this potential error, I'm using another class I wrote. The class "globals" is simply a config-loader. That is, a configuration file ('globals.php - config file') exists in application/config and contains a series of custom $config array elements. One of these elements is $default_controller (as specified in Routes.php). The "globals" (config-loader) class has been added to CI's Autoload, takes the globals-config-file as its argument and simply outputs each of the $config array elements as 'global variables', using $CI->load->vars().

In my nav-link function, I'm trying to access $default_controller so that I can equate it with base_url(). That is, if uri_string() returns EITHER "" OR $default_controller, the 'current-page' is obviously "root url" (or base_url()) and no anchor is needed on the "Home" list-element.

Now..

The problem I'm having is that I am unable to gain access to $default_controller from within my helper function. I don't understand why that is or how to remedy the situation. I even tried accessing my custom config file to pull the $default_controller value through $CI->config->item($default_controller) which didn't work either.

I hope you find this post more sensibly written, I certainly took the time and put in the effort.

Thanks for all the help. I really can't overstate how much this has contributed to integrating CI into my workflow and my growing love for it!
#5

[eluser]kaejiavo[/eluser]
Daniel,
It really couldn't be more complicated!

CI comes with a config loader. You use it like this:
Code:
$this->config->load('filename');
If you need the config globally, just autoload it...
Quote:User_guide:
If you find that you need a particular config file globally, you can have it loaded automatically by the system. To do this, open the autoload.php file, located at application/config/autoload.php, and add your config file as indicated in the file.
To retrieve an item from your config file, use the following function:
Code:
$this->config->item('item name');

There is really no need for a library that calls a helper invoking a global class. Wink

Marco

P.S. All of this info comes directly from the user_guide chapter config class, no insider know how.
#6

[eluser]stormbytes[/eluser]
Marco,

Thanks for the tip, but I really dont' think you understood the point of my post.

The config-loader class loads all of the config array-indices as global variables. This is independent of any 'helper'. The helper only makes use of the global vars. In my case, as I described in my follow-up post, $this->config->item($whatever) cannot access the config-value I'm looking for and my question was related to this problem.

The purpose of the config-loader class is specifically so you don't have to $this->config->item($whatever) each time you need to access a config value.
#7

[eluser]WanWizard[/eluser]
Where do you get the idea that they are loaded as global variables? They are not.

The config file is included inside the load() method of the config class, so the $config variable is automatically local to that method. It then stores the values in a property of the config class, which you access through $this->config->item().
#8

[eluser]stormbytes[/eluser]
Marco,

I just tried loading the config file using $CI->load->config() prior to accessing the index, and it worked! I'm glad you posted the tip as this half-solves my problem, though my original question still stands. Why can't the helper access pre-loaded variables? ($this->load->vars($var_name)). They are certainly accessible from the View, and the helper function is being called from the View. Weird..

A follow up question -

Do you know if it's possible to pick up the value of default controller from Routes.php programmatically somehow?

Cheers!
#9

[eluser]stormbytes[/eluser]
[quote author="WanWizard" date="1288148401"]Where do you get the idea that they are loaded as global variables? They are not.

The config file is included inside the load() method of the config class, so the $config variable is automatically local to that method. It then stores the values in a property of the config class, which you access through $this->config->item().[/quote]


WanWizard,

I'm not suggesting that 'config variables' are globals. What I said about 'global variables' was in reference to variables, config-vars or otherwise, that were loaded using $this->load->vars($whatever). In this case, would not $whatever be a global?
#10

[eluser]WanWizard[/eluser]
Please be careful how you phrase things.

When you talk about global variables in PHP, there is only one meaning, namely the one described in the PHP manual. So if you talk about global variables, any PHP programmer will assume you mean those. We can't guess what you exactly mean...




Theme © iAndrew 2016 - Forum software by © MyBB