[英]PHP Cannot redeclare function/Call to undefined function
I am using WooCommerce on Wordpress 4.7.1. 我在Wordpress 4.7.1上使用WooCommerce。 Previously, I was running 4.7.0.
以前,我正在运行4.7.0。 When Wordpress automatically upgraded to 4.7.1, I suddenly began seeing checkouts fail to redirect to the "Thank you" page, and the UI showed that a fatal error had occurred.
当Wordpress自动升级到4.7.1时,我突然开始发现检出失败无法重定向到“谢谢”页面,并且UI显示发生了致命错误。
Turning on debugging revealed the following error: 打开调试显示以下错误:
PHP Fatal error: Cannot redeclare get_cc() (previously declared in .../templates/emails/email-addresses.php:36) in .../templates/emails/email-addresses.php on line 36
PHP致命错误:无法在第36行的... / templates / emails / email-addresses.php中重新声明get_cc()(先前在... / templates / emails / email-addresses.php:36中声明)
I also confirmed that this file is never directly called with require
, require_once
, include
, or include_once
. 我还确认了绝对不会直接使用
require
, require_once
, include
或include_once
调用此文件。 It is, however, called with wc_get_template()
. 但是,使用
wc_get_template()
调用。
A quick search brought me to this similar question . 快速搜索使我想到了类似的问题 。 So I added the following to email-addresses.php:
因此,我将以下内容添加到email-addresses.php中:
if (!function_exists('get_cc')) {
function get_cc( $code ) {
... function body
}
}
After making this change, I now receive the following error in the debug log: 进行此更改后,我现在在调试日志中收到以下错误:
PHP Fatal error: Call to undefined function get_cc() in .../templates/emails/email-addresses.php on line 32
PHP致命错误:在第32行的... / templates / emails / email-addresses.php中调用未定义的函数get_cc()
This means that, for whatever reason, PHP believes that the function get_cc
already exists, but at the same time it is undefined. 这意味着,无论出于何种原因,PHP都认为函数
get_cc
已经存在,但同时未定义。
I was not the original developer. 我不是原始开发人员。 I took over the project from a contractor who is not available.
我从没有承包商的承包商那里接管了该项目。 It does seem clear that this file is heavily modified from the original.
显然,该文件与原始文件相比经过了重大修改。 I am certain that the value returned by the function must be kept, so I cannot just comment out the call.
我确定必须保留该函数返回的值,因此我不能只注释掉该调用。
Given the information above, what options do I have to either workaround or fix this issue? 鉴于上述信息,我必须采取哪些解决方案或解决此问题?
It turns out that, unlike a standard function definition in PHP, function definitions wrapped in if (!function_exists('function_name')) { ... }
block must precede any call to that function . 事实证明,与PHP中的标准函数定义不同,
if (!function_exists('function_name')) { ... }
块中包装的函数定义必须在对该函数的任何调用之前 。
In this case, the function definition (line 36) followed the call (line 32). 在这种情况下,函数定义(第36行)位于调用之后(第32行)。 This caused the function to appear to PHP as undefined:
这导致该函数在PHP中显示为未定义:
// Not working!
$value = get_cc($code);
if (!function_exists('get_cc')) {
function get_cc( $code ) {
...
}
}
Switching the order so that the function definition came first fixed the issue: 切换顺序以使函数定义首先出现可解决此问题:
// Working!
if (!function_exists('get_cc')) {
function get_cc( $code ) {
...
}
}
$value = get_cc($code);
As of right now, PHP's documentation does not mention this issue. 截至目前,PHP的文档尚未提及此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.