php require_once tries to include a second time on

2019-05-14 23:51发布

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?

7条回答
劳资没心,怎么记你
2楼-- · 2019-05-15 00:29

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';

查看更多
叼着烟拽天下
3楼-- · 2019-05-15 00:30

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.

查看更多
我命由我不由天
4楼-- · 2019-05-15 00:34

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.

查看更多
男人必须洒脱
5楼-- · 2019-05-15 00:37

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!!

查看更多
男人必须洒脱
6楼-- · 2019-05-15 00:39

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.

查看更多
Animai°情兽
7楼-- · 2019-05-15 00:45

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.

查看更多
登录 后发表回答