Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Preface: I am working in PHP (Abandon hope all ye who enter here).

Background: There exists a large set of global functions in PHP, a number of which are miscellaneous system calls, like sleep (and others). Now, I use sleep (and others) in a bunch of different scripts I run in a bunch of different places, and I have found I need sleep to call pcntl_signal_dispatch as soon as the sleep finishes- but possibly not in all my scripts.

A Generalization: I need to make global function do more than it currently does, hopefully without disrupting my current ecosystem too much.

My Solution: I figure I could create a wrapper class that executes the correct "sleep".

Singleton: I could make a singleton "System" class that wraps the global functions. Hell, I could even make the wrappers static methods. The downside is that there would be a lot of boilerplate checking to see which version I would need to execute, either a vanilla function call or one with extra stuff.

Global variable: I could make a generic "System" class that wraps the global functions. I could then extend the System class with different classes that override the wrapper functions. I create a global System variable within each script, dependent upon how I need the functions to behave. All my scripts have access to that global variable. The downside is I would have to make sure the global variable is declared, is never overwritten, and uses the proper System.

Something else: I could create a SysControl class with a static System variable and static wrappers of the System's wrappers of the functions, and then swap out which System my SysControl class references. The downside is that I feel I am going overboard.

Are there any more options I should consider? Which of these methods is the best, and why? What pitfalls should I look for going forward?

EDIT: I ended up using the Something else solution.

share|improve this question
Does PHP not have first-class functions? I'm not versed enough in PHP to actually answer, but stay away from singletons, globals, or static variables. Invisible dependencies are not good. Find a way to pass in the right function to your scripts without requiring someone to mutate some global state before calling it. – Doval Apr 15 '14 at 0:48
@Doval The problem is, if I pass in functions as arguments, that will get very out of hand very quickly. – MirroredFate Apr 15 '14 at 0:54
More quickly than using a Singleton as a switch yard? – Robert Harvey Apr 15 '14 at 3:49
@RobertHarvey Quite possibly, yes. If I have functions A, B, C, and D, and A calls B which calls C which calls D, and I have to pass through four functions in addition to their normal arguments, and I have this situation everywhere, that does not seem maintainable at all. Far less maintainable, in fact, than using a Singleton or global... I total agree with Doval about invisible dependencies- they have the tendency to cause headaches- but other solutions seem worse (hence the question). – MirroredFate Apr 15 '14 at 17:24
Would the use of namespaces actually increase the mess, or provide another alternative ? – mika Apr 16 '14 at 8:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.