是否可以替换 php 中的函数(例如邮件)并使其执行其他操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1837184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to replace a function in php (such as mail) and make it do something else?
提问by chris
I want to rewrite a function in PHP (let's say the mail() function), and want to make it so when I call mail() from now on, it will load my version of mail() and not the default php version. Is this possible in php?
我想用 PHP 重写一个函数(比如说 mail() 函数),并且希望从现在开始调用 mail() 时,它会加载我的 mail() 版本而不是默认的 php 版本。这在php中可能吗?
The reason I want to do this is because I have thousands of lines of code which call mail() and I don't want to rewrite all of them.
我想这样做的原因是因为我有数千行调用 mail() 的代码,我不想重写所有这些代码。
Also for future reference, in computer programming, what is it called when you do something like this?
也供将来参考,在计算机编程中,当你做这样的事情时,它叫什么?
回答by Milan Babu?kov
There is an extension that allows you to override functions. It is meant to be used for debugging, but I guess you can use it for your needs. Take a look:
有一个扩展允许您覆盖功能。它旨在用于调试,但我想您可以根据需要使用它。看一看:
http://www.php.net/manual/en/function.override-function.php
http://www.php.net/manual/en/function.override-function.php
If you wish to call the original function within your version, make sure you read this comment: http://no.php.net/manual/en/function.override-function.php#50821
如果您希望在您的版本中调用原始函数,请务必阅读以下评论: http://no.php.net/manual/en/function.override-function.php#50821
回答by Tim Lytle
What you're referring to is generally called method overloading.
您所指的通常称为方法重载。
While not generally supported by PHP, you actually may be able to redefineinternal functions (as well as user functions) using runkit_function_remove()or runkit_function_redefine(). Of course to use that, you'll need to have pretty much full control over the PHP installation - since it's not bundled with PHP you'll need to install the runkit extension.
虽然 PHP 通常不支持,但您实际上可以使用runkit_function_remove()或重新定义内部函数(以及用户函数)runkit_function_redefine()。当然,要使用它,您需要完全控制 PHP 安装 - 因为它没有与 PHP 捆绑在一起,您需要安装 runkit 扩展。
Again, in a normal situation internal functionsas well as user functions cannot be redefined(or overloaded) in PHP. This situation illustrates the benefit of wrapping some internal functions with a user function.
同样,在正常情况下,内部函数和用户函数不能在 PHP 中重新定义(或重载)。这种情况说明了用用户函数包装一些内部函数的好处。
回答by Pekka
回答by raveren
You can use regexps to replace the function name in your files. What app you use is up to you, but I'd recommend the ovely-pricey Powergrep(unless you won't need it again).
您可以使用正则表达式替换文件中的函数名称。您使用什么应用程序取决于您,但我建议您使用非常昂贵的Powergrep(除非您不再需要它)。
replace
代替
(\s+)mail([\s(]+)
to
到
new_function_name
and
和
^mail([\s(]+)
to
到
new_function_name
Sorry for my regexp-fu, I don't know how to search for either spaces OR begining of line in one expression.
抱歉我的 regexp-fu,我不知道如何在一个表达式中搜索空格或行首。

