1

I have a PHP program I'm writing which is about 200 lines of code. It has many functions I wrote, perhaps a dozen. I want to have a debug option in the program, but want that value to be accessible within all the functions as well. How and where should this be defined?

Global $debug_status;

function blah ($message) {
if ($debug_status == "1" ) {
  do something...}
...
}

Is this the right approach? Thanks!

Edward
  • 9,430
  • 19
  • 48
  • 71

3 Answers3

3

Use a constant.

define('DEBUG', true);

...

if (DEBUG) ...

There are of course better ways to debug. For example, use OOP, inject a logger instance into each of your objects, call

$this->logger->debug(...);

to log messages, switch the output filter of the logger to show or hide debug messages.

deceze
  • 510,633
  • 85
  • 743
  • 889
1

You were almost there .... the global keyword imports a reference to a global into the current scope.

$debug_status = "ERROR";

function blah ($message) {
    global $debug_status;
    if ($debug_status == "1" ) {
      do something...}
      ...
    }
samayo
  • 16,163
  • 12
  • 91
  • 106
Orangepill
  • 24,500
  • 3
  • 42
  • 63
  • So you must define the variable global inside each function? – Edward Jun 04 '13 at 06:23
  • correct... another option is you can access it from the globals super global via `$GLOBALS["debug_status"]` but both of these techniques completely ignore scope and the benifits that it provides. – Orangepill Jun 04 '13 at 06:25
1

The variable should be defined in Registry class which is sort of pattern.

Working demo

Example of Registry

class Registry {
   private static $registry = array();

   private function __construct() {} //the object can be created only within a class.
   public static function set($key, $value) { // method to set variables/objects to registry
      self::$registry[$key] = $value;
   }

   public static function get($key) { //method to get variable if it exists from registry
      return isset(self::$registry[$key]) ? self::$registry[$key] : null;
   }
}

Usage

To register object you need include this classn

$registry::set('debug_status', $debug_status); //this line sets object in **registry**

To get the object you can use get method

$debug_status = $registry::get('debug_status'); //this line gets the object from **registry**

This is solution that every object/variable can be stored in. For such purpose as you wrote it's good to use simple constant and define().

My solution is good for every kind of object that should be accessed from anywhere in application.

Edit

Removed singleton and make get, set methods as static as @deceze suggested.

Robert
  • 19,800
  • 5
  • 55
  • 85
  • 1
    Why not make the whole `Registry` and all its methods `static`? Why go through the trouble of statically getting an instance to get keys, why not `Registry::get('debug_status')` directly? Seems like the worst of both worlds. → [How Not To Kill Your Testability Using Statics](http://kunststube.net/static/). – deceze Jun 04 '13 at 06:32
  • Obviosly it can be done using static methods but there is no difference in working both solutions will work. What you suggest is shorter which maybe be better :) – Robert Jun 04 '13 at 06:34
  • I've changed the solution using static methods as @deceze suggested. – Robert Jun 04 '13 at 06:40