Page 1 of 2
Which is the best technic to use global arrays & variables ?
Posted: Tue Nov 11, 2008 11:09 am
by kanenas.net
I am using this...
Code: Select all
<?php
$arrTEST = array('xxx', 'yyy', 'zzz');
function myTest() {
global $arrTEST;
foreach ($arrTEST as $value) {
echo $value . "<br />";
}
}
myTest();
?>
Which outputs..
Will this feature be DEPRECATED or REMOVED in php v6?
Is there another way to use global arrays?
Which is the best technic to use "
global arrays" and "
global variables"?
Thanks.
Re: Which is the best technic to use global arrays & variables ?
Posted: Tue Nov 11, 2008 12:03 pm
by Mark Baker
kanenas.net wrote:Will this feature be DEPRECATED or REMOVED in php v6?
Is there another way to use global arrays?
Which is the best technic to use "global arrays" and "global variables"?
The best technique is not to use globals at all
Re: Which is the best technic to use global arrays & variables ?
Posted: Tue Nov 11, 2008 1:15 pm
by josh
Code: Select all
<?php
$arrTEST = array('xxx', 'yyy', 'zzz');
function myTest( $arr ) {
foreach ($arr as $value) {
echo $value . "<br />";
}
}
myTest( $arrTEST );
?>
Re: Which is the best technic to use global arrays & variables ?
Posted: Tue Nov 11, 2008 3:58 pm
by kanenas.net
Actually the questions were...
Is there another way to use global arrays?
Which is the best technic to use "global arrays" and "global variables"?

Re: Which is the best technic to use global arrays & variables ?
Posted: Tue Nov 11, 2008 4:12 pm
by requinix
kanenas.net wrote:Actually the questions were...
Is there another way to use global arrays?
Which is the best technic to use "global arrays" and "global variables"?

And we're telling you not to do it. Care to explain why it's so important?
Re: Which is the best technic to use global arrays & variables ?
Posted: Tue Nov 11, 2008 4:32 pm
by Stryks
You could use define() if you really must, and you can store arrays in there if you serialize() and then unserialize()
Not the best option unless you really want to make a constant global variable.
Re: Which is the best technic to use global arrays & variables ?
Posted: Tue Nov 11, 2008 5:12 pm
by josh
kanenas.net wrote:Actually the questions were...
Is there another way to use global arrays?
Which is the best technic to use "global arrays" and "global variables"?

Sorry, I didn't realize bad design was part of your project requirements. To answer your question then the only ways to use global variables is to declare it global or access it through the $_GLOBALS super array. You could also use a registry which exploits an OOP feature called static variables to simulate what a global variable would do. If you really must use globals I would recommend a registry because then at least youre only dynamicaly coupling your code to the global variable, and not statically coupling it.
http://en.wikipedia.org/wiki/Global_variable
http://en.wikipedia.org/wiki/Singleton_pattern
http://en.wikipedia.org/wiki/Static_variable
Re: Which is the best technic to use global arrays & variables ?
Posted: Wed Nov 12, 2008 3:40 am
by kanenas.net
jshpro2 wrote:
Sorry, I didn't realize bad design was part of your project requirements. To answer your question then the only ways to use global variables is to declare it global or access it through the $_GLOBALS super array. You could also use a registry which exploits an OOP feature called static variables to simulate what a global variable would do. If you really must use globals I would recommend a registry because then at least youre only dynamicaly coupling your code to the global variable, and not statically coupling it.
It is very important for me to use gloabal arrays & variables.
This is my project...
I have 22 variables or arrays that must be passed or used in 40+ different functions.
I know the technic to pass the vars like:
myfunction (v1,v2,...v22){
//code here
}
but it is a lot of code and not very easy to read.
(the project is a generic algorithm functionality in order to find forecast for tv viewers ratings)
Any suggestions ?!
Re: Which is the best technic to use global arrays & variables ?
Posted: Wed Nov 12, 2008 4:06 am
by requinix
kanenas.net wrote:Any suggestions ?!
Martyr yourself and do it the right way? We're giving suggestions, you just don't like them.
I'm all in favor of using OOD ideas when they're called for, but how about take a step back: use one array to store all the information. One array, 22 elements (or however many there are).
There are a number of ways of doing this the wrong way; jshpro2 mentioned some, I mentioned another. Pick one and go with it.
Re: Which is the best technic to use global arrays & variables ?
Posted: Wed Nov 12, 2008 5:56 am
by mmj
kanenas.net wrote:I am using this...
Code: Select all
<?php
$arrTEST = array('xxx', 'yyy', 'zzz');
function myTest() {
global $arrTEST;
foreach ($arrTEST as $value) {
echo $value . "<br />";
}
}
myTest();
?>
Which outputs..
Will this feature be DEPRECATED or REMOVED in php v6?
Is there another way to use global arrays?
Which is the best technic to use "
global arrays" and "
global variables"?
Thanks.
What is wrong with what you are currently doing?
Re: Which is the best technic to use global arrays & variables ?
Posted: Wed Nov 12, 2008 10:41 am
by josh
mmj wrote:What is wrong with what you are currently doing?
http://www.google.com/search?q=why+not+ ... =firefox-a
Results 1 - 10 of about
9,900,000 for why not to use global variables. (0.15 seconds)
Basically top down programming, monolithic 1 giant source code file was the way to program 40 years ago. Global variables violate a lot of the very principles responsible for structured programming. Use of global variables creates coupled, non cohesive, hard to debug, error prone, difficult to maintain, difficult to understand code. You may argue that you don't realize the negative consequences first hand because your project is small or you're just writing one function or something, which is moot. If you just want it to work thats different then if you want to program correctly.
Re: Which is the best technic to use global arrays & variables ?
Posted: Wed Nov 12, 2008 11:23 am
by mmj
jshpro2 wrote:mmj wrote:What is wrong with what you are currently doing?
http://www.google.com/search?q=why+not+ ... =firefox-a
Results 1 - 10 of about
9,900,000 for why not to use global variables. (0.15 seconds)
Basically top down programming, monolithic 1 giant source code file was the way to program 40 years ago. Global variables violate a lot of the very principles responsible for structured programming. Use of global variables creates coupled, non cohesive, hard to debug, error prone, difficult to maintain, difficult to understand code. You may argue that you don't realize the negative consequences first hand because your project is small or you're just writing one function or something, which is moot. If you just want it to work thats different then if you want to program correctly.
Yeah, but obviously you are going to need
some global variables.
Re: Which is the best technic to use global arrays & variables ?
Posted: Wed Nov 12, 2008 11:27 am
by infolock
global variables? I haven't used since 2004 i think.
only global variables i use are superglobal variables

Re: Which is the best technic to use global arrays & variables ?
Posted: Wed Nov 12, 2008 11:48 am
by alex.barylski
global variables? I haven't used since 2004 i think.
They can come in handy, just like multiple inheritence or GOTO. It's when they are used zealously or carelessly that they become a eye sore and/or problem.
Re: Which is the best technic to use global arrays & variables ?
Posted: Wed Nov 12, 2008 12:22 pm
by mmj
infolock wrote:global variables? I haven't used since 2004 i think.
only global variables i use are superglobal variables

So you don't have one variable that can be accessed in the global scope?
Not possible.