简体   繁体   English

我可以覆盖另一个文件/插件中调用的PHP函数吗?

[英]Can I override a PHP Function that is called in another file/plugin?

First of, I have read " Override default php function " and namespaces doesn't fulfil my need. 首先,我已阅读“ 覆盖默认的php函数 ”,并且名称空间无法满足我的需求。 I have also looked into PHP.net's override_function() but that can't help me with my problem either. 我也研究了PHP.net的override_function(),但这也无法帮助我解决问题。

I am using a Wordpress plugin called Jigoshop as an eCommerce solution but in some cases I cannot remove the actions I need to apply my own structure to a 'single product' page. 我正在使用名为Jigoshop的Wordpress插件作为电子商务解决方案,但在某些情况下,我无法删除将自己的结构应用于“单个产品”页面所需的操作。 I do not want to edit the plugin files themselves as a plugin update may negate and remove my previous changes. 我不想自己编辑插件文件,因为插件更新可能会否定并删除我以前的更改。 Essentially, I want to control the output through my /themes/mytheme/functions.php file. 本质上,我想通过我的/themes/mytheme/functions.php文件控制输出。

Has anyone come across this before whereby the original function is contained in a file I do not want to edit for that same 'updating' reason? 有人遇到过这个问题,因为出于同样的“更新”原因,原始功能包含在我不想编辑的文件中吗?

Thanks 谢谢

EDIT (2012-11-21): I have a custom function in my functions.php file like so: 编辑(2012-11-21):我在functions.php文件中有一个自定义函数,如下所示:

function prepare_jigoshop_wrappers() {
    remove_action('jigoshop_before_main_content', 'jigoshop_breadcrumb', 20);
    add_action('jigoshop_before_main_content', 'custom_jigoshop_breadcrumb', 10);
}
add_action('wp_head', 'prepare_jigoshop_wrappers');

This essentially allows me to apply my own structure & configuration. 从本质上讲,这使我可以应用自己的结构和配置。 For other default functions, it is a little more difficult. 对于其他默认功能,要困难一些。 For example, the 'quantity selector', 'Add to Cart' button and 'stock availability' all are contained within a function called jigoshop_template_single_summary in the jigoshop_template_actions.php file calling the likes of _title , _price , _excerpt , _meta , _sharing & _add_to_cart . 例如,“数量选择器”,“添加到购物车”按钮和“库存状况”全部包含在jigoshop_template_actions.php文件中名为jigoshop_template_single_summary的函数中,该函数调用jigoshop_template_single_summary_title_price_excerpt_meta_sharing_add_to_cart

The order these are displayed, I cannot seem to change. 这些显示的顺序,我似乎无法更改。 Therefore, I want to essentially redefine function jigoshop_template_single_summary() {...} 因此,我想从本质上重新定义function jigoshop_template_single_summary() {...}

You may want to look into PECL extension. 您可能需要研究PECL扩展名。 It may do what you need, docs on PHP.NET. 它可以满足您的需要,PHP.NET上的文档。

runkit_function_redefine runkit_function_redefine

(PECL runkit >= 0.7.0) (PECL Runkit> = 0.7.0)

I don't know this plugin very well but probably this is what you Need Theming with Jigoshop 我不太了解这个插件,但是可能这是您需要使用Jigoshop进行主题化的功能

Override some functions in php(also in common..) is never a good idea. 重写php中的某些功能(也是共同点。)从来都不是一个好主意。 If the plugin doesn't provide a way to hook into it's functionality is sometimes better to use an alternative plugin or rewrite the core code.. 如果插件没有提供挂钩功能的方法,有时最好使用替代插件或重写核心代码。

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

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