I wonder if it is possible to replace var_dump with some user defined function. I know that you can use any kind of dumping functions of various modules, or some wrappers. But what I want to achieve is that anyone in my project who uses "var_dump", gets my new function, without "knowing" it and without the need to use a different syntax. Simply override the funcion. Thanks
1 Answer
PHP doesn't support re-declaring functions AFAIK. However there's a little trick you can do on a case specific basis.
Say you have this code in a file:
var_dump($a);
var_dump($b);
var_dump($c);
You can just wrap this in a namespace like so:
namespace OverridingGlobalNamespace {
function var_dump($_) {
echo "My custom var_dump";
}
var_dump($a); //Will use namespace function instead of PHP function
var_dump($b);
var_dump($c);
}
4 Comments
Asped
Hi, this seems to be an interesting solution, i have to check if this really works ;)
Asped
as you say - this is case specific, I still cannot apply it to all var_dump calls. So I guess it IS impossible :)
apokryfos
@Asped I don't claim this to be an ideal solution or even a good idea. I just find it to be an interesting quirk of the language. Ideally you'd want to not use var_dump but your own wrapper function which does what you want it to do (and which you can define differently on debug and production environments).
Asped
sure an own wrapper is the best solution. the only thing i intended to do was to ensure on a live project, that if there are some dumps which are done accidentaly or whatever, that they are suppressed, when used in a wrong way
function vd($var) { echo '<pre>'; var_dump($var); echo '</pre>'; }. I suggest you that.