可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have this code at the top of various include files:
require_once("functions.php");
Sometimes I need to include several of my include files to generate a page and on my local server this works fine as the code above tells it to only include functions.php once (hence it doesn't attempt to declare functions twice).
When I upload to my production server, suddenly it tries to include functions.php a second time, resulting in a fatal error from attempting to redeclare the functions a second time.
Is there something in my php config on my production server that would be causing require_once to behave differently?
回答1:
You can use the following code:
require_once(realpath(dirname(__FILE__) . '/functions.php'));
and modify the path location before functions.php
if functions.php
is located in another directory in root.
The function realpath(dirname(FILE) takes path of root folder. So you should include your function in your index.php and modify if your function.php in any directory such as:
require_once (realpath(dirname(__FILE__) . '/your_directory_name/functions.php'));
Enjoy!!
回答2:
Is it possible you have duplicate files named functions.php?
Your example represents a relative location, is it possible you are loading it twice?
Maybe to debug it would be
echo __FILE__
from within the functions file.
回答3:
Check your path settings, if the function names are common, it's likely another file with the same name is getting loaded. In addition to echoing the mentioned __FILE__
value, just try naming it something you know is unique and/or use an absolute path like
require_once dirname(__FILE__).'/functions.php';
回答4:
Maybe the error handling level is set differently on the 2 servers?
require_once will simply return true when called more than once, if I remember correctly.
There is some weird results possible with this construct:
Loading (even a fake) file twice with include_once, the return value will be true.
Example:
<?php
var_dump(include_once 'fakefile.ext'); // bool(false)
var_dump(include_once 'fakefile.ext'); // bool(true)
?>
This is because according to php the file was already included once (even though it does not exist).
Not sure if this is the same with require_once but it may well be since the docs refer to include_once for expected behavior of this construct.
回答5:
In a case-insensitive file system, the require_once language construct will point to the same file regardless of any capitalization in the path. However, and this is the catch, although the different capitalization variants point to the same file, they are not treated as the same file when the check is made to determine if the file has already been included.
require_once __DIR__.'/address/src/Coordinates.php';
require_once __DIR__.'/Address/src/Coordinates.php';
In the above example, in a case-insensitive file system, both lines will attempt to require the same file. The second file will succeed because there is difference in the capitalization of the path.
This gets worse if you have further require_once language constructs in the required files, i.e. you are doing multilevel requires, combined with relative paths. In such a scenario, the nested relative path will be resolved to a full path using the capitalization used in higher level requires. If anywhere in this nested structure you use a different capitalization, you will encounter the same problem.
var_dump(get_included_files());
This will give you a list of included files with their full paths and exact capitalization you used. This will help you find the colprit quickly.
回答6:
I honestly can't think of anything that would cause this behavior, if I knew a bit about how your site was structured I could offer some suggestions on how to structure it differently so this wouldn't happen.
Something that I do on my sites is have this sort of setup:
global.php <- stuff like configuring the database, including a file with random functions
header.php <- basic html header stuff, global.php is required_once here.
Then in all the other files, just require the header file and you should have no conflicts. If you're using a specific framework (CakePHP, Kohana), then this definitely changes things...like I said, not enough info to go on right away.
回答7:
If you are including files that contain classes, you may wish to try using __autoload
, which should prevent multiple includes/requires from running, and prevents you from listing a bunch of includes/requires.