Welcome Guest, Not a member yet? Register   Sign In
global in helper-functions
#1

[eluser]Unknown[/eluser]
Hi,

i'm just trying to write my own helper, but somehow the global-keyword doesn't seem to work in my helper-files?

A short example of my helper-file:
Code:
<?php
$test = 'This is a test';

function test() {
  global $test;
  var_dump($test);
}

var_dump($test);
test();
?>

The first var_dump works as expected, the value of $test is shown... But the second one, the one inside the function just outputs NULL... global doesn't seem to work, but there isn't a parse_error or any kind of php-message...

If i try the same code outside of code-igniter and load the file directly everythings works as it should...

Where is my thinking-error? Wink
#2

[eluser]Dam1an[/eluser]
What are you trying to do? You don't really need globals in most cases (I personally never liked them anyway)
#3

[eluser]Unknown[/eluser]
Well, i try to implement a global variable (at least it should be available in the complete helper) with the absolute path to this helper. The first idea which came to my mind was solving this by using a constant, the problem is, that i want to make that path as variable as possible (different installations, etc).

Because of that i decided against he use of a constant but for a variable, that could be changed whenever needed...

But even if I would use a constant, the question still would be open: why doesn't the global-keyword work in this case?
#4

[eluser]TheFuzzy0ne[/eluser]
Where are you planning on defining this global variable? I've never yet found a situation where global variables are necessary. I'm not saying they never are necessary, I just can't think of a situation when you would.
#5

[eluser]Unknown[/eluser]
gotta love when ppl, instead of replying, just say "you don't need it anyway" ...

I currently have the same problem which totally prevent me from implementing a couple of others application into my CodeIgniter's one ... (sorry I'm not gonna call the developers of bbpress and tell them to remove all their global variables because anyway global is 'bad')

I'm still trying to fix it but here is a hint someone just gave me on stackoverflow.com :

copy paste of his answer :

"The Input class in Codeigniter (which is automatically loaded):

* Destroys the global GET array. Since CodeIgniter does not utilize GET strings, there is no reason to allow it.
* Destroys all global variables in the event register_globals is turned on.
* Filters the POST/COOKIE array keys, permitting only alpha-numeric (and a few other) characters.
* Provides XSS (Cross-site Scripting Hacks) filtering. This can be enabled globally, or upon request.
* Standardizes newline characters to \n

Maybe you can rip that bit out, and just make sure you have register_globals off, or do some crafty code shuffling.

The input class is loaded in the file system/codeigniter/CodeIgniter.php and I have no idea what would happen if you removed it.
"
#6

[eluser]bretticus[/eluser]
No need to try and remove the Input class from execution. It runs before the controller is called and couldn't possibly remove this variable because the $test variable has yet to be set. Just to be sure I commented out:

Code:
$this->_sanitize_globals();

...in the CI_Input controller. Guess what? Null value is still set for test variable.

I think this is a language issue. Possibly the result of included files loading included files, etc. I see nothing in the helper method of the load class other than a simple include_once statement.

I recommend using a config file anyways for getting values to adhere with CI best practises:

Code:
function test() {
$CI =& get_instance();
$this->config->load('test_config');
return $CI->config->item('test');
}
#7

[eluser]TheFuzzy0ne[/eluser]
[quote author="pinpin_lelapin" date="1252493176"]gotta love when ppl, instead of replying, just say "you don't need it anyway" ...[/quote]

That's because in this instance, you don't.

[quote author="pinpin_lelapin" date="1252493176"]
I currently have the same problem which totally prevent me from implementing a couple of others application into my CodeIgniter's one ... (sorry I'm not gonna call the developers of bbpress and tell them to remove all their global variables because anyway global is 'bad')[/quote]

Sorry, I must be missing something here (I guess it's my old age). Who said globals are bad?

What does BBPress have to do with this, anyway? Is this not the CodeIgniter forums? My point is that in CodeIgniter, I have never found the need to use global variables. Global variables are instead stored in either the config class, or neatly in another library/object where they can be accessed easily. One of the beauties of OOP is that it allows you to work with such variables more cleanly, and reduces the chances of you ending up with a naming clash.




Theme © iAndrew 2016 - Forum software by © MyBB