Global Variables in PHP

  • Bill_Thompson
  • Student
  • Student
  • User avatar
  • Posts: 82

Post 3+ Months Ago

Is it possible to have true global variables in PHP? The tests I have done so far seem to suggest that you can have a global variable that is global only to the php file that declares it and not to your whole website. Once you go to another php file, the variable seems to loose what you assign it.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

One place to start looking is at something like memcache.
  • Bill_Thompson
  • Student
  • Student
  • User avatar
  • Posts: 82

Post 3+ Months Ago

This is something that has to be installed on the server, right?
I think I will have to ask my hosting provider if they have this extension since it does not come with the default PHP package.

Maybe another option is just to hand the client a cookie.

Maybe another option is to grab the users IP address and use that as an entry into a MYSQL table as a way to ID them.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

The need for a global variable indicates a design flaw. What are you intending to use it for?
  • Bill_Thompson
  • Student
  • Student
  • User avatar
  • Posts: 82

Post 3+ Months Ago

Spork,

The Universe itself is a design flaw where huge systems have adapted plug-ins and customized tweaks are required to make the system behave as you envision. Redesigning the Universe is not a solution. It might be a resolution, but it is not a solution in the real world.

I have been struggling to get the open source php based CMS, WordPress, running exactly as I want it to. At first, I figured I would have to write my own plug-in. But after a long search I found a couple of plug-ins which, if they did exactly what they were supposed to do, would suit my needs.

But they were not as flexible and not as robustly written. I could scrap them and write my own from scratch, but then I noticed that if I just has a global variable, it would do the trick I needed.

Aren't cookies pseudo-globals to a website? If their need is a design flaw, the world is flawed and they exist to fill a need.

=============

jobert, they do not offer memcache, :(
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

Thanks for the insight into the inner workings of the universe. :thumbsup:

You should've mentioned WordPress at the beginning; I was under the assumption that you were writing a web app yourself. The WordPress codebase is already a steaming pile of awful.

To my knowledge there isn't any way to set a persistent global in PHP; however you should be able to set up class auto-loading, then just create a singleton class containing whatever properties and other information you need. This will let you avoid explicitly including an extra file wherever you need the property.

WordPress might already have class auto-loading enabled, but I'm not sure :scratchhead:
  • Bill_Thompson
  • Student
  • Student
  • User avatar
  • Posts: 82

Post 3+ Months Ago

I think I will try cookies to satisfy my global hunger. I wonder if the inventor of broswer cookies had this in mind. It seems like a kind of global variable.

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 69 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.