0

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

2
  • You need to edit the PHP source to achieve what you want. :-) Commented Mar 21, 2016 at 13:19
  • I do always write the function function vd($var) { echo '<pre>'; var_dump($var); echo '</pre>'; }. I suggest you that. Commented Mar 21, 2016 at 13:29

1 Answer 1

1

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); 
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, this seems to be an interesting solution, i have to check if this really works ;)
as you say - this is case specific, I still cannot apply it to all var_dump calls. So I guess it IS impossible :)
@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).
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.