Is it possible to replace a function in php (e.g. mail) and make it do something else?

I want to rewrite the function in PHP (say, the mail () function), and I want to do this when I call mail () from now on, it will load my version of mail (), not the default php version. Is this possible in php?

I want to do this because I have thousands of lines of code that call mail (), and I don’t want to rewrite them.

Also for further use in computer programming, what is called when you do something like this?

thank

+18
function php
Dec 03 '09 at 1:56
source share
4 answers

There is an extension that allows you to redefine functions. It is intended for debugging, but I think you can use it for your needs. Take a look:

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

If you want to call the original function in your version, be sure to read this comment: http://no.php.net/manual/en/function.override-function.php#50821

+19
Dec 03 '09 at 2:40
source share

What you mean is usually called method overloading .

While PHP is usually not supported, you can really override internal functions (as well as user functions) using runkit_function_remove() or runkit_function_redefine() . Of course, for this you will need to completely control the installation process of PHP - since it is not associated with PHP, you need to install the runkit extension .

Again, in a normal situation, internal functions , as well as user functions, cannot be overridden (or overloaded) in PHP. This situation illustrates the benefits of porting some internal functions using a user-defined function.

+9
Dec 03 '09 at 2:09
source share

It caused function overloading and is not possible in native PHP, but it is possible to use the extensions described in other answers. The PHP documentation claims this is not possible at all: a source that is incorrect.

+2
Dec 03 '09 at 2:00
source share

You can use regular expressions to replace the function name in your files. What you use is up to you, but I would recommend ovely-pricey Powergrep (unless you need it again).

replace

  (\s+)mail([\s(]+) 

to

  $1new_function_name$2 

and

  ^mail([\s(]+) 

to

  new_function_name$1 

Sorry for my regexp-fu, I don't know how to look for spaces OR start a line in a single expression.

0
Dec 03 '09 at 2:34
source share



All Articles