Jump to content

Global variables in PHP: a solution


Recommended Posts

From Experts-Exchange newsletter :

Global variables are a fact of life. Although global variables are technically never necessary, particularly in PHP programs that use object-oriented design, they often make programs easier to read and easier to write. There are definitely situations in which they are reasonable and appropriate: resources or handles returned from API calls that are used in many functions, frequently-used arguments passed in via GET or POST, and very long strings used in cryptological protocols are some cases that come to mind.

The problem I have with global variables in PHP is that they work the opposite way they do in other languages: they are declared in each function, instead of at file level. This is about as verbose and difficult to maintain as passing each global variable used by a function in its argument list. Argument lists (or "global" statements) become longer, and must change wherever needed when a new global variable is added to the program.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.