require_once with relative paths

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
small_mark
Forum Newbie
Posts: 2
Joined: Sat Apr 17, 2004 7:44 pm

require_once with relative paths

Post by small_mark »

I have separated my code into various directories, is the relative path taken from the current file i.e. the file the require_once statement resides, or from the very first php file that is used.

for instance I have a UserManager class which uses a User class, I also have a test php file which instantiates the UserManager in a different directory again (the filesystem structure is below!!!). From the command line (in the test directory) I call 'php testUserManager.php', I get the error message specified below.

The required file(s) from the UserManager class should be one directory up and then into the data directory for the User class and into the collection directory for the Vector class but this does not work.

The way I got it to work is by going up one directory then into the classes directory then finally into either the data or collection directory.

This to me seems wrong, it appears that the base from which the relative paths are working is from the test directory where my test code resides instead of being relative to where the class that is requiring the file.

Obviously my question is is this true and why???????

Filesystem Structure:
C:\
Program Files\
Apache Group\
Apache2\
htdocs\
test\
testUserManager.inc.php
classes\
authentication\
UserManager.inc.php
collection\
Vector.inc.php
data\
User.inc.php

[ERROR STATEMENT]
C:\Program Files\Apache Group\Apache2\htdocs\base>php testUserManager.php
PHP Warning: main(../User.inc.php) [http://www.php.net/function.main]: failed to create stream: No such file or directory in C:\Program Files\Apache Group\Apac
he2\htdocs\classes\authentication\UserManager.inc.php on line 4
PHP Fatal error: main() [http://www.php.net/function.main]: Failed opening required '../User.inc.php' (include_path='.;c:\php4\pear') in C:\Program Files\Apach
e Group\Apache2\htdocs\classes\authentication\UserManager.inc.php on line 4


[UserManager.inc.php FILE]

Code: Select all

<?PHP

//require_once("../collection/Vector.inc.php"); // DOESN'T WORK....
//require_once("../User.inc.php");              // DOESN'T WORK....
require_once("../classes/collection/Vector.inc.php"); // WORKS....
require_once("../classes/data/User.inc.php");         // WORKS....

class UserManager &#123;
&#125;
?>
[/b]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: require_once with relative paths

Post by feyd »

Code: Select all

C:\
  Program Files\
    Apache Group\
      Apache2\
       htdocs\
         test\
           testUserManager.inc.php
         classes\  
           authentication\
             UserManager.inc.php
           collection\
             Vector.inc.php
           data\
             User.inc.php
It seems perfectly fine to me. The relative path is from the file executing the code, not from the one including it. At least, that's how I take it.. and that's how it looks to work.

Think of it this way: include, include_once, require, and require_once just do a byte dump of whatever is in each of the files until there are no more to include.
small_mark
Forum Newbie
Posts: 2
Joined: Sat Apr 17, 2004 7:44 pm

Post by small_mark »

Thanks for the prompt reply.

You say that
The relative path is from the file executing the code, not from the one including it.
Maybe I missunderstand but certainly in my code the file executing the code is the file including it???

Also the error message I get says there is a problem with the require_once statement in the UserManager class not the base test class which I would expect going by what you say in your reply. If as you say it is just a byte dump e.g. the UserManager class would dump all code from the Vector and User classes into its binary file and the testUserManager would dump everything in the UserManager class (including the Vector and User classes) into the testUserManager file

Also I notice that in my code my UserManager uses both a User and a Vector class of my own design. Another inconsistency is that I have commented out the require_once Vector statement and it still runs fine whereas if I comment out the User require_once statement I get an error about instantiating a non-existing class. This appears to be inconsistent as niether class is specified or used anywhere else??

Mark
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Okay, I've done some testing with these.. first the directory layout:

Code: Select all

folder1/
  this.php
  folder2/
    this.php
    folder3/
      this.php
each consisting roughly of:

Code: Select all

{require/include}("../this.php");
echo __FILE__."\n";
(minus the top one level one.) with includes, all the files get added correctly. With requires, php crashes due to circular inclusion. i.e. require does a straight byte dump, only resolving includes/requires based on relative to the original file including them.. i.e. the deepest this.php.
Post Reply