Which is the best technic to use global arrays & variables ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
kanenas.net
Forum Newbie
Posts: 12
Joined: Sun Jun 26, 2005 12:29 pm
Location: Athens - Greece
Contact:

Which is the best technic to use global arrays & variables ?

Post 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..

Code: Select all

 
xxx
yyy
zzz
 
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.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Which is the best technic to use global arrays & variables ?

Post 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
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Which is the best technic to use global arrays & variables ?

Post by josh »

Code: Select all

 
<?php
$arrTEST = array('xxx', 'yyy', 'zzz');
 
function myTest( $arr ) {
 
  foreach ($arr as $value) {
     echo $value . "<br />";
  }
}
 
myTest( $arrTEST );
?>
 
User avatar
kanenas.net
Forum Newbie
Posts: 12
Joined: Sun Jun 26, 2005 12:29 pm
Location: Athens - Greece
Contact:

Re: Which is the best technic to use global arrays & variables ?

Post 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"?


:wink:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Which is the best technic to use global arrays & variables ?

Post 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"?


:wink:
And we're telling you not to do it. Care to explain why it's so important?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Which is the best technic to use global arrays & variables ?

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Which is the best technic to use global arrays & variables ?

Post 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"?


:wink:
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
User avatar
kanenas.net
Forum Newbie
Posts: 12
Joined: Sun Jun 26, 2005 12:29 pm
Location: Athens - Greece
Contact:

Re: Which is the best technic to use global arrays & variables ?

Post 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 ?!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Which is the best technic to use global arrays & variables ?

Post 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.
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Which is the best technic to use global arrays & variables ?

Post 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..

Code: Select all

 
xxx
yyy
zzz
 
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?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Which is the best technic to use global arrays & variables ?

Post 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.
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Which is the best technic to use global arrays & variables ?

Post 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.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Which is the best technic to use global arrays & variables ?

Post by infolock »

global variables? I haven't used since 2004 i think.

only global variables i use are superglobal variables ;)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Which is the best technic to use global arrays & variables ?

Post 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.
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Which is the best technic to use global arrays & variables ?

Post 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.
Post Reply