PHP - 覆盖现有功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4725194/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:09:25  来源:igfitidea点击:

PHP - override existing function

phpwordpressfunction

提问by Alex

Can I redeclare a existing function, with the same name, but different code? Or somehow "disable" the old function?

我可以重新声明一个现有函数,名称相同但代码不同吗?或者以某种方式“禁用”旧功能?

I want to redefince a core WordPress function, but since plugins and theme call this function a lot, I need to keep the same function name.

我想重新定义一个核心 WordPress 功能,但由于插件和主题经常调用此功能,因此我需要保持相同的功能名称。

采纳答案by coreyward

Only if you use something like APD that extends the zend engine to allow for that:

仅当您使用像 APD 这样扩展 zend 引擎的东西来允许这样做时:

Intro

介绍

Override Method Docs

覆盖方法文档

Note: Runkit seems like a better option than APD since it's more specific to this purpose and would allow you to keep the original method intact at a different address.

注意:Runkit 似乎是比 APD 更好的选择,因为它更具体地用于此目的,并且允许您在不同的地址保持原始方法完好无损。

回答by rachael

You could use the WordPress hooks (called filters and actions) and then use the add_filter()function to override the function you are using.

您可以使用 WordPress 挂钩(称为过滤器和操作),然后使用该add_filter()函数覆盖您正在使用的函数。

i.e.

IE

function function_name() {
 //code goes here
}

$hook = 'get_options'; // the function name you're filtering
add_filter( $hook, 'function_name' );

The Codex will help a lot with this.

Codex 将在这方面提供很大帮助。

http://codex.wordpress.org/Plugin_API/Filter_Reference

http://codex.wordpress.org/Plugin_API/Filter_Reference

回答by NikiC

Just comment the old function out and write your new one ;)

只需将旧函数注释掉并编写新函数即可;)

In PHP it is not possible to redefine or overload (i.e. define a function with the same name but different parameters) a function natively. There though are extensions like runkitwhich allow to redefine functions (runkit_function_redefine), but you probably don't want to use these (such extensions are rarely installed and mostly unreliable.)

在 PHP 中,无法重新定义或重载(即定义具有相同名称但不同参数的函数)本机的函数。虽然有像runkit这样的扩展允许重新定义函数 ( runkit_function_redefine),但您可能不想使用这些扩展(此类扩展很少安装,而且大多不可靠。)

回答by DeaconDesperado

You can wrap the block defining the original function in a conditional checking if another of the same name is not already defined (I'm assuming you mean Wordpress functions and not core PHP ones)

如果尚未定义另一个同名函数,您可以将定义原始函数的块包装在条件检查中(我假设您指的是 Wordpress 函数而不是核心 PHP 函数)

    <?php 
if(!function_exists('function_name')){ 
    //old definition here
    } ?>

You could then redefine it above while still preserving the original should you need to roll back to it.

然后,您可以在上面重新定义它,同时在需要回滚时仍保留原始文件。

Depending on how complex the changes are and how many times you may do this, you may also want to look into Namespacesif you are on PHP 5.

根据更改的复杂程度以及您可以执行此操作的次数,如果您使用的是 PHP 5 ,您可能还需要查看命名空间

回答by Andrew Seabrook

My attempted solution was to do this:

我尝试的解决方案是这样做:

function suffusion_get_image($options = array()) {
    include_once ABSPATH.'wp-content/themes/suffusion-child/functions/media.php';
    return childtheme_overide_suffusion_get_image($options = array());
    ....
}

Obviously there is an overhead at upgrade as you would need to add lines back into the scripts again and I have used this method successfully to date but now trying to do it with get_termsin the wp-includes and hitting a redeclaration issue which I am trying to resolve or workaround at the moment.

显然,升级会产生开销,因为您需要再次将行添加回脚本中,迄今为止我已经成功使用了此方法,但现在尝试get_terms在 wp-includes 中执行此操作并遇到了我正在尝试的重新声明问题目前解决或解决方法。

My reason to edit core is that the existing core does not provide in a convenient way for a multisite requirement.

我编辑核心的原因是现有核心不能以方便的方式提供多站点需求。

Someone has just suggested on another forum however using override_functionbut the manual is worded such that it appears to be of use only to built-in functions - I took it that means PHP built in functions

有人刚刚在另一个论坛上建议使用,override_function但手册的措辞似乎仅对内置函数有用 - 我认为这意味着 PHP 内置函数