php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28775 Require_once fails on diff. files in different dirs with same name & path
Submitted: 2004-06-14 17:18 UTC Modified: 2004-06-28 11:59 UTC
Votes:2
Avg. Score:2.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: aldo at pepperstream dot nl Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.6 OS: SuSe
Private report: No CVE-ID: None
 [2004-06-14 17:18 UTC] aldo at pepperstream dot nl
Description:
------------
I hope this bug has not already been reported but I have the following directory structure:

root
 \_ src
 |_ plugins
     \_ [nameplugin]
           \_ src

(so a src directory in both root and in a plugin two directories further up)

In a file in the root I refer to:
require_once('src/library.php'); 

and I do the same in the root/plugins/[nameplugin] directory:
require_once('src/library.php'); 

Now the second library.php refuses to be included. It referes to two different files, but the include looks similar due to the relative path's being used. 

When I renamed the second library.php to library2.php a test all my functions and also some test code started working. 

Reproduce code:
---------------
root/index.php
require_once('src/library.php'); 

root/plugins/[nameplugin]/[nameplugin].php

require_once('src/library.php'); 



Expected result:
----------------
I expected both library.php's who exist on different path's to work and not just the one to be included, as both files are actually different, only their names and the relative path to differ.

Actual result:
--------------
second library.php was not included.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-06-14 19:46 UTC] iliaa@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc.

If possible, make the script source available online and provide
an URL to it here. Try avoid embedding huge scripts into the report.

Please provide a complete PHP script needed to reproduce 
the problem. 
 [2004-06-16 09:12 UTC] aldo at pepperstream dot nl
Of course. You can find a script that triggers this possible bug at: http://www.vorm.net/bug28775.tar.gz
 [2004-06-18 19:04 UTC] imprestavel at gameguru dot com dot br
At the url you just said, you gave an example using include, and not include_once/require_once.
I suspect that you have the wrong idea of how include works.

http://www.php.net/include/

Second paragraph:
"Files for including are first looked in include_path relative to the current working directory and then in include_path relative to the directory of current script. E.g. if your include_path is ., current working directory is /www/, you included include/a.php and there is include "b.php"  in that file, b.php is first looked in /www/  and then in /www/include/."
 [2004-06-22 15:51 UTC] aldo at pepperstream dot nl
Then the script I put there is wrong. I will upload a new one shortly. It should have set include_once and not include. I will add a new reply with a new script that should simulate the bug. My applogies for the mistake.
 [2004-06-22 17:09 UTC] jochem at pepperstream dot nl
I updated the script at http://vorm.net/bug28775.tar.gz to use require_once() instead of include().

Problem is the same, and behaviour of include() and require_once() is (and should, according to the manual) be the same in this respect. So I think the quoted part of the manual still applies.

So perhaps this behaviour is by design (and not a bug), BUT it is problematic in large projects. It should in my opinion actually behave as C does in this respect, which means the example code should display 'Hello World' instead of 'function not defined'.

I would appreciate if others would look at this and test if it is actual a bug and/or behaviour that can be changed.
 [2004-06-28 11:49 UTC] rasmus@php.net
That's not a require_once problem.  You are confused about what the current working directory is as your script is executing.  All includes are relative to the top script, so even if you include module/module.php and in the include inc/lib.php this will be top/inc/lib.php and not top/module/inc.php.  This is so you can move things around and have it work the same no matter where you put things.  As long as you keep this in mind and always include relative to the top, everything should work a lot better for you. ;)
 [2004-06-28 11:59 UTC] aldo at pepperstream dot nl
I understand what you mean and it might not be a "bug" but it's really annoying and confusing to work with. Because if I at a lower level in my structure include something there with a name not found in a higher level it WILL work. It only stops working if it can find a match earlier on. So basically your left with 2 "working directories" (not really, but for sake of argument). 

If you use the same name as found on a top level it will get the one from the top level and if the name can't be found it will also look through the further down level from where the second file with a second include statement is and match files found there.

You have to admid it at least is very confusing to work with :)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 19 01:00:03 2025 UTC