简体   繁体   English

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

[英]How to set multiple PHP classes intoa Debug mode?

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? 轻松将10个以上的PHP类设置为Debug模式,但仍保持该类与其他内容无关的最佳方法是什么?

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. 这意味着如果我尝试在另一个项目中使用该类,则它依赖于另一个文件中的Constant。

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. 如果您有10个以上的类,并且想让它们依赖于其他东西一起处于“调试”状态,那么您就无法真正使它们完全独立。 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. 我可以想到两种方式:使用define和使用环境变量。

I'd use a define : 我会用一个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. 另一种可能性是依赖于环境变量,但是如果您对允许使用的环境变量没有把握,则在safe_mode下可能会失败。 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; 然后,可以使用getenv检索“ MYPROJECTNAME_DEBUG”环境变量; it will be false if it can't be found. 如果找不到,它将为假。

getenv('MYPROJECTNAME_DEBUG');

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. 如果我让所有这些类都检查名称空间特定的调试常量,则可以将我正在处理的代码(非系统代码)设置为“调试”模式。

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. 我使用一个常量IS_PRODUCTION ,当它设置为false时 ,意味着我怀疑所有警告都会显示出来并输出有关故障的更多信息...此常量包含在每页中。

PS I hope that I understood your question correctly... 附言:我希望我能正确理解您的问题...

根据调试模式的含义,您可能会对Assertions感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM