-1

Wonder if there's some way to wrap PHP default function response in another function or prepend and postpend function response with strings. I wan tto do it without creating custom function, but instead changing default function output e.g. add tag around var_dump() output.

$my_var = "Hello World!";
var_dump($my_var);

Outputs Hello World!

I want

$my_var = "Hello World!";
var_dump($my_var);

Outputs <strong>Hello World!</strong>

I am not interested in creating and calling custom function to do this, instead i want modify var_dump() output.

EDIT: I need native solution without installing PHP extensions

Roman Toasov
  • 747
  • 11
  • 36
  • No, PHP offers no way of "overloading" functions in the standard language. – IMSoP Dec 11 '14 at 23:40
  • Possible duplicate of [Is it possible to replace a function in php (such as mail) and make it do something else?](http://stackoverflow.com/questions/1837184/is-it-possible-to-replace-a-function-in-php-such-as-mail-and-make-it-do-someth); or [Redefining PHP functions?](http://stackoverflow.com/questions/2640958/redefining-php-function); or [Is it possible to replace (monkeypatch) PHP functions?](http://stackoverflow.com/questions/530649/is-it-possible-to-replace-monkeypatch-php-functions?lq=1) or many others found by a quick search. – IMSoP Dec 11 '14 at 23:44
  • As pointed out in some of the existing answers, e.g. http://stackoverflow.com/a/18120190/157957, you can define a function of the same name in a namespace, e.g. `namespace Awesome { function var_dump() { ... }`, then with `use Awesome;` at the top of each file, PHP will see any call to `var_dump($foo);` as actually meaning `Awesome\var_dump($foo);` – IMSoP Dec 11 '14 at 23:55

1 Answers1

2

You can use runkit_function_rename if you install the runkit PECL extension.

You will have to place some code before any var_dumps are called but you can create your own var_dump after renaming the existing var_dump.

I do think this is a terrible idea though, and you've much better off writing your own function as a wrapper for var_dump and refactoring your code to use your new function.

Otherwise, what you wish to do is not possible with a native PHP installation.

Scopey
  • 6,269
  • 1
  • 22
  • 34
  • Thanks, but i need native solution if there's any. – Roman Toasov Dec 11 '14 at 23:39
  • Then it's not possible I'm sorry. – Scopey Dec 11 '14 at 23:41
  • @RomanToasov This is the best answer you're going to get, sorry. The answer to "can this be done without an extension?" is simply "no". – IMSoP Dec 11 '14 at 23:41
  • There should be way because i successfully used PECL extensions locally without installation to server, just downloaded and included in my project using `require_once()` with slight modification of paths. Perhaps if i look at it source i can find how they doing this. – Roman Toasov Dec 11 '14 at 23:42
  • @RomanToasov A PECL extension is compiled C code that interacts with the internals of the language; you cannot simply include it or replicate it in PHP code. – IMSoP Dec 11 '14 at 23:47
  • @RomanToasov Feel free to look at the source, though. In the case of `runkit_function_redefine`, it's here: http://lxr.php.net/xref/PECL/runkit/runkit_functions.c#638 – IMSoP Dec 11 '14 at 23:56
  • Yeah i see this one is in C other one i used was just bunch of PHP classes. Never mind then i will just create custom function with var_dumpz(); or smth. – Roman Toasov Dec 12 '14 at 00:07