views:

55

answers:

2

Hi there, I was wondering if anyone could offer some advice on 'best practices' for using global state in a web application - specifically PHP, although im looking for generic best practices i.e. design patterns etc.

At the moment I just use a static class, calling it Configs. I suppose this is similar to using the registry pattern but surely there is a more elegant way of handling global data within an application - i just cant think of a better way though.

A: 

If you're only concerned with managing the global scope inside of each request, a simple Registry pattern is sufficient.

Zend_Registry works fine if you want some library code to lean on, and can be used without the rest of the Zend Framework stuff.

Or you can roll your own class if you prefer, as you say you're doing now.

timdev
so, the registry pattern is considered best practice for handling global data then?
David
So long as you can't make that global data non-global, Registry is arguably a best practice.
timdev
A: 

I'd say, in the order of appearance:

  • avoid global state if possible (e.g. using dependency injection)

If not possible, use:

  • registry pattern
  • singleton pattern
takeshin