简体   繁体   English

我可以扩展内置的PHP函数吗?

[英]Can I Extend a Built-In PHP Function?

Long story short I'm working with a legacy codebase that makes heavy use of PHP's built-in call_user_func_array function. 总而言之,我正在使用一个遗留代码库,该遗留代码库大量使用了PHP的内置call_user_func_array函数。 I'm trying to get it to work with PHP 5.3 (upgrading from 5.2), but have run into the issue described here (in the "Passing non-arrays" section): 我正在尝试使其与PHP 5.3(从5.2升级)一起使用,但是遇到了此处描述的问题(在“传递非数组”部分中):

http://sixohthree.com/1486/migrating-to-php-5-3-call_user_func_array http://sixohthree.com/1486/migrating-to-php-5-3-call_user_func_array

In a nutshell, the problem is that between PHP versions 5.2 and 5.3 the bevavior of this function was changed so that it basically does nothing and returns NULL if the second parameter is not a proper array/object/associative array. 简而言之,问题在于在PHP 5.2和5.3版本之间,此函数的行为已更改,因此,如果第二个参数不是正确的数组/对象/关联数组,则该函数基本上不执行任何操作并返回NULL 5.2 did not do this, and as such the codebase I'm working with makes no effort to ensure that it passes a parameter of the correct type. 5.2并没有这样做,因此我正在使用的代码库不做任何努力来确保它传递正确类型的参数。 This causes problems. 这会引起问题。

To fix it I could follow the instructions in the blog post and hunt down every single call_user_func_array call in the codebase and patch them up, but that would be extremely tedious. 要解决此问题,我可以按照博客文章中的说明进行操作,并在代码库中查找每个call_user_func_array调用并进行修补,但这非常繁琐。 Alternately, I noticed that PHP has a built-in override_function API call that can be used to override the built-in functions. 另外,我注意到PHP有一个内置的overlay_function API调用,可用于覆盖内置函数。 That's handy, but what I want to do is more like extending the built-in function. 那很方便,但是我想做的更像是扩展内置功能。

Ideally what I'd like is to be able to replace the implementation of call_user_func_array with something roughly like: 理想情况下,我希望能够用大致类似的内容代替call_user_func_array的实现:

function call_user_func_array($method, $params) {
    $params = is_array($params) ? $params : array($params);
    return old_call_user_func_array($method, $params);
}

...where old_call_user_func_array is the built-in call_user_func_array function. ...其中old_call_user_func_array是内置的call_user_func_array函数。

Is this possible, and if so, how? 这可能吗?如果可以,怎么办?

You can use rename_function which is also in the APD extension so you should already have it if you have override_function installed: 您可以使用APD扩展名中的named_function ,因此,如果已安装override_function,则应该已经拥有它:

rename_function('call_user_func_array', 'old_user_func_array');

function call_user_func_array($method, $params) {
    $params = is_array($params) ? $params : array($params);
    old_call_user_func_array($method, $params);
}

Hi your question is actually answered in the third or fourth comment posting in the online PHP documentation. 您好,您的问题实际上在在线PHP文档中的第三或第四条评论中得到了回答。 There is often very useful information and examples in the comments section (mind you there is also somtimes stuff that is patently incorrect!) I've copied the relevant portion here for your convenience: 在注释部分中通常有非常有用的信息和示例(请注意,还有一些有时是明显不正确的somtimes内容!)为了方便起见,我在此处复制了相关部分:

... if you use rename_function to rename the original function to a third name, then call the third name in the OVERRIDING function, you will get the desired effect: ...如果使用rename_function将原始函数重命名为第三个名称,然后在OVERRIDING函数中调用第三个名称,则将获得所需的效果:

rename_function('strlen', 'new_strlen'); named_function('strlen','new_strlen');

override_function('strlen', '$string', 'return override_strlen($string);'); override_function('strlen','$ string','returnoverride_strlen($ string);');

function override_strlen($string){ return new_strlen($string);} 函数override_strlen($ string){返回new_strlen($ string);}

or you can put your code in a namespace: 或者您可以将代码放在命名空间中:

namespace Phpoverride
{
    function call_user_func_array($method, $params) {
        return \call_user_func_array($method, $params);
    }
}

http://php.net/namespaces http://php.net/namespaces

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM