Search code examples
phpdebuggingvar-dump

Replace var_dump with a custom function in PHP


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


Solution

  • 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); 
    }