tags:

views:

71

answers:

4

What would be the best way to set 10+ PHP classes into Debug mode easily but still keeping the classes non-dependent of other stuff?

If I set a Global Constant then check for it's value inside every class then every class is reliant on this constant being set. Meaning if I try to use the class in another project it is relying on a Constant from another file.

A: 

I use a constant *IS_PRODUCTION* which, when is set to false, means that I suspect all the warnings to be shown and to output additional information about the failures...this constant is included on every page.

P.S. I hope that I understood your question correctly...

rATRIJS
+2  A: 

You could do something like:

define("DEBUG", TRUE);

and then

if(defined("DEBUG"))
{
    // .... debug code
}

This would set your entire script into debug mode

You could use Class Constants instead. (or namespace constants if your into that)

class MyClass{
    const Debug = true;

    public function do()
    {
          if(self::Debug == true)
          {
               // .. debug stuff
          }
    }
}

Then you just change the constant based on the status.

This is actually VERY powerful when using namespaces, as I keep all my 'system' specific files (logger, database connection, etc) in its own namespace. If I have all of those classes check for namespace specific debug constants, I can JUST have the code I'm working on (the not-system code) be in Debug mode.

Chacha102
In the class example you posted, would it have to be set in every class? Or can it be set globally to turn on/off all classes debug?
jasondavis
A: 

Depending on what you mean by debug mode you might be interested in Assertions.

VolkerK
+3  A: 

Isn't the question somewhat contradictory? If you have 10+ classes and you want to have them to depend on something else together to be in a 'debug' state, then you can't really keep them completely independent. The point is that you will necessarily be dependent of something; I think your best take is to choose what's the least entangling solution.

I can think of two ways: using a define and using an environment variable.

I'd use a define:

define('DEBUG', true);

And in each of my class files, before declaring the class, I'd check if the constant exists:

if(!defined('DEBUG')) define('DEBUG', $my_default_debug_value);

So they can still work in a standalone fashion, but you're still dependent on a constant.


Another possibility would be to rely on an environment variable, but that could fail under safe_mode if you have no grip over which environment variables are allowed. I personally wouldn't use it because I don't like them, but maybe it's just what you're looking for.

putenv('MYPROJECTNAME_DEBUG=1');

Then getenv can be used to retrieve the 'MYPROJECTNAME_DEBUG' environment variable; it will be false if it can't be found.

getenv('MYPROJECTNAME_DEBUG');
zneak
Thanks the getenv is something I wasn't aware of, I'll have to check it out, also your other idea might be the key as well
jasondavis