简体   繁体   English

我正在使用include_once但仍然无法重新声明。 我知道函数存在,这就是我使用include_once的原因

[英]I am using include_once but still getting Cannot redeclare. I know the function exists, that is why I'm using include_once

Ok I am trying to modify some code in a WP Plugin. 好的,我正在尝试修改WP插件中的一些代码。 The plugin declares a file for this: 该插件为此声明了一个文件:

// overwrite hooks 
$plugin_dir = trailingslashit(dirname(dirname(__FILE__)));
if(file_exists($plugin_dir . "auto_overwrite.php")){
include_once($plugin_dir . "auto_overwrite.php");
}

So I created a file in the above directory called auto_overwrite.php and included the function I wanted to change. 所以我在上面的目录中创建了一个名为auto_overwrite.php的文件,并包含了我想要更改的函数。

The problem I am having is that I still get the error: 我遇到的问题是我仍然得到错误:

Fatal error: Cannot redeclare listing_tabs() (previously declared in.... 致命错误:无法重新声明listing_tabs()(之前在....中声明)

I thought the idea of include_once was that is would not include functions that are already defined. 我认为include_once的想法是不包括已经定义的函数。

The other function is in a file named meta_boxes.php and it too is called via 另一个函数位于名为meta_boxes.php的文件中,它也被称为via

include_once("meta_boxes.php");

I am relatively new to PHP so maybe I'm missing something, any help is greatly appreciated. 我对PHP比较新,所以也许我错过了什么,非常感谢任何帮助。 Thanks! 谢谢!

EDIT: From answers below I see that I cant just call out the function as include_once only limits that specific file from being loaded more than once. 编辑:从下面的答案我看到我不能只调用函数include_once只限制特定文件被加载多次。 Basically I am trying to modify a function specified downstream from this file. 基本上我正在尝试修改此文件下游指定的函数。 Is it possible to do this? 是否有可能做到这一点?

You can check if a function has been defined, and if not act accordingly like this... 您可以检查是否已定义某个功能,如果没有这样做,则...

 if (!function_exists('listing_tabs')){
    // include file with function
 }

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

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