dragon8899 2010-02-07 15:50
浏览 4
已采纳

如何将多个PHP类设置为调试模式?

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.

  • 写回答

4条回答 默认 最新

  • doutao6380 2010-02-07 19:28
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?