user1122069
user1122069

Reputation: 1807

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. 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.

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.

Upvotes: 2

Views: 708

Answers (4)

beiller
beiller

Reputation: 3135

Is it possible to use the disable_functions operation in php.ini to disable var_dump on your production server? 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"

Also there is override_function:

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

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

Upvotes: 5

Adam
Adam

Reputation: 18807

You can use monkey patching.

Just defines a namespace on the first line of your file and defines the function 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()

Of course, all others native function will continue to work as usual as long as you do not override them in your namespace.

Upvotes: 0

Marcus Olsson
Marcus Olsson

Reputation: 2527

There are actually ways to do this, if you have PECL available and runkit installed. You kan make runkit able to overide PHPs internal functions if you in php.ini set runkit.internal_override to "1".

For removing the var_dump function, you could use:

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.

You may also want to take a look at "Advanced PHP debugger", another extension that seems to offer an override_function().

Upvotes: 0

Andreas
Andreas

Reputation: 25

Why don't you use serialize() or json_encode() if you have a large database? That will be very useful.

But take note, serialize() will give you a 1-line output somewhat like this:

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

So you need to learn the anatomy of serialize() to use it: PHP Serialize

Upvotes: -1

Related Questions