neoform Posted November 16, 2007 Share Posted November 16, 2007 is there a way to make a custom superglobal like $GLOBALS that is available in any namespace without having to use "global $varname" in each namespace.. ? I'm currently using $GLOBALS['varname'] but it's pretty ugly. Link to comment https://forums.phpfreaks.com/topic/77634-custom-superglobals/ Share on other sites More sharing options...
Orio Posted November 16, 2007 Share Posted November 16, 2007 What's wrong with using: global $var1, $var2... ??? Orio. Link to comment https://forums.phpfreaks.com/topic/77634-custom-superglobals/#findComment-392971 Share on other sites More sharing options...
cooldude832 Posted November 16, 2007 Share Posted November 16, 2007 you could write a pseudo function <?php function globals_NOT($var){ return $_GLOBALS['var']; } ?> Link to comment https://forums.phpfreaks.com/topic/77634-custom-superglobals/#findComment-392973 Share on other sites More sharing options...
neoform Posted November 16, 2007 Author Share Posted November 16, 2007 What's wrong with using: global $var1, $var2... ??? Orio. It's nice to be able to have access toa variable like $_POST without having to declare it as a global each time you want to use it. Link to comment https://forums.phpfreaks.com/topic/77634-custom-superglobals/#findComment-393003 Share on other sites More sharing options...
Psycho Posted November 16, 2007 Share Posted November 16, 2007 I think you are on the right track. Just create an array, such as your $GLOBALS and just declare that as a global when you need it: global $GLOBALS; Then you have access to all thos pseudo global variables with just one global variable. Link to comment https://forums.phpfreaks.com/topic/77634-custom-superglobals/#findComment-393008 Share on other sites More sharing options...
cooldude832 Posted November 16, 2007 Share Posted November 16, 2007 its not a smart idea to do any of that as you are using resources beacuse you are lazy. Link to comment https://forums.phpfreaks.com/topic/77634-custom-superglobals/#findComment-393060 Share on other sites More sharing options...
Orio Posted November 16, 2007 Share Posted November 16, 2007 Don't be lazy when it comes to programming. This will you will start programming with short_tags enabled, register_globals and who knows what other bad-practice settings. You want to make your scripts as clean, readable and system-compatible as possible. If the variables are something that should be global, declare them as global at the start of your functions. If it's not something that you function needs but it's not something very important (by something important I mean stuff such as a DB connection, some kind of setting etc'), simply pass it as a parameter to the function. Of course, if these variables have a constant value, define them as constants with define(). Orio. Link to comment https://forums.phpfreaks.com/topic/77634-custom-superglobals/#findComment-393077 Share on other sites More sharing options...
neoform Posted November 16, 2007 Author Share Posted November 16, 2007 Err, there's nothing wrong with using short_tags.. it's not bad code ethic since i've never seen a machine with php5 that doesn't have it enabled. I use $GLOBALS right now to store a number of objects like my $GLOBALS['sql'] object that contains the mysqli object or $GLOBALS['tpl'] which is template object, both of which get used in pretty much every page on the site, this is exactly what superglobals were meant for, I just find it an ugly var name and would rather using something like $_SQL or $_TPL. Either way, using superglobals like this is not a bad practice.. I find "global $varname" to be far less clean. Link to comment https://forums.phpfreaks.com/topic/77634-custom-superglobals/#findComment-393104 Share on other sites More sharing options...
Orio Posted November 17, 2007 Share Posted November 17, 2007 Using short_tags or register_globals is bad practice. There are many machines that have them disabled, and it's not so difficult to write "<?php" instead of "<?" and "<?php echo" instead of "<?=". Except that, you are always in a risk of causing problems with xml (that uses <?xml). If you are looking for something "clean", define these important variables in an array called $_SQL or $_TPL or whatever, and at the start of every function use global on them. I did that alot in projects I have done before- it's clean, clear and easy to use. Orio. Link to comment https://forums.phpfreaks.com/topic/77634-custom-superglobals/#findComment-393352 Share on other sites More sharing options...
Psycho Posted November 17, 2007 Share Posted November 17, 2007 If you are looking for something "clean", define these important variables in an array called $_SQL or $_TPL or whatever, and at the start of every function use global on them. I did that alot in projects I have done before- it's clean, clear and easy to use. Isn't that what I said? Link to comment https://forums.phpfreaks.com/topic/77634-custom-superglobals/#findComment-393589 Share on other sites More sharing options...
Orio Posted November 17, 2007 Share Posted November 17, 2007 You said superglobal, I said you should declare them global at the start of your functions. Orio. Link to comment https://forums.phpfreaks.com/topic/77634-custom-superglobals/#findComment-393608 Share on other sites More sharing options...
Psycho Posted November 18, 2007 Share Posted November 18, 2007 You said superglobal, I said you should declare them global at the start of your functions. No I didn't. I said Just create an array, such as your $GLOBALS and just declare that as a global when you need it Link to comment https://forums.phpfreaks.com/topic/77634-custom-superglobals/#findComment-393681 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.