简体   繁体   English

从PHP代码中删除var_dump

[英]Removing var_dump from PHP code

We have a large codebase, and every so often a var_dump used for testing and not removed/commented suddenly appears out of nowhere. 我们有一个庞大的代码库,并且经常会突然出现一个用于测试并且未被删除/注释的var_dump突然出现。 There is a messy solution using XDebug ( http://devzone.zend.com/1135/tracing-php-applications-with-xdebug/ ), but maybe there's something ingenous that can be done in PHP at runtime. 使用XDebug有一个凌乱的解决方案( http://devzone.zend.com/1135/tracing-php-applications-with-xdebug/ ),但也许可以在运行时用PHP进行一些巧妙的处理。

Also, I don't want to modify or search code via regex. 另外,我不想通过正则表达式修改或搜索代码。 I've tried using my own var_dump_v2, but it falls out of use quickly. 我尝试使用自己的var_dump_v2,但很快就无法使用。

Is it possible to use the disable_functions operation in php.ini to disable var_dump on your production server? 是否可以在php.ini中使用disable_functions操作禁用生产服务器上的var_dump? I am not sure what the outcome of this setting is (ie does it fail with an error, or silently) the documentation is not so clear. 我不确定此设置的结果是什么(即,它是否由于错误而失败,还是无提示)文档尚不清楚。

http://php.net/manual/en/ini.core.php - see "disable_functions" http://php.net/manual/zh/ini.core.php-参见“ disable_functions”

Also there is override_function: 另外还有override_function:

<?php
override_function('var_dump', '$a', 'return 0;');
?>

http://php.net/manual/en/function.override-function.php http://php.net/manual/en/function.override-function.php

There are actually ways to do this, if you have PECL available and runkit installed. 如果您有可用的PECL并安装了runkit ,实际上有多种方法可以执行此操作。 You kan make runkit able to overide PHPs internal functions if you in php.ini set runkit.internal_override to "1". 如果在php.ini中将runkit.internal_override设置为“ 1”,则可以使runkit能够覆盖PHP的内部函数。

For removing the var_dump function, you could use: 要删除var_dump函数,可以使用:

runkit_function_remove('var_dump');

In your case, not to get an error, you should probably instead use something like this: 在您的情况下,为了避免出错,您可能应该改用如下方式:

runkit_function_redefine('var_dump', '','');

Take a look at the runkit extensions documentation here . 这里查看runkit扩展文档

You may also want to take a look at " Advanced PHP debugger ", another extension that seems to offer an override_function() . 您可能还想看看“ Advanced PHP debugger ”,这是另一个似乎提供了override_function()的扩展。

You can use monkey patching. 您可以使用猴子修补程序。

Just defines a namespace on the first line of your file and defines the function var_dump 只需在文件的第一行定义一个名称空间,然后定义函数var_dump

     <?php
     namespace monkey;
     function var_dump($obj) {}

Of course, it implies that you do not use a namespace in your current file 当然,这意味着您不在当前文件中使用名称空间

You could use the function var_dump() prefixing it with the root namespace(): \\var_dump() 您可以使用函数var_dump()为它加上根名称空间(): \\var_dump()

Of course, all others native function will continue to work as usual as long as you do not override them in your namespace. 当然,所有其他本机函数将继续照常工作,只要您不在命名空间中覆盖它们即可。

Why don't you use serialize() or json_encode() if you have a large database? 如果数据库很大,为什么不使用serialize()json_encode() That will be very useful. 那将非常有用。

But take note, serialize() will give you a 1-line output somewhat like this: 但是请注意, serialize()将为您提供一线输出,如下所示:

's:0:"";s:5:"value";'

So you need to learn the anatomy of serialize() to use it: PHP Serialize 因此,您需要学习serialize()的用法才能使用它: PHP Serialize

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

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