Re: [PHP] best way to set up an include path for a multi-level project?

[email protected] (John Black)
Newsgroups php.general
Message-ID <[email protected]>
On 03/16/2010 06:57 PM, Robert P. J. Day wrote:
>    i have a project (let's call it "proj") which lives in the "proj"
> directory and which contains several subdirs, some of which might
> contain their own subdirs and so on.  some of those subdirs might
> contain utility classes that i want to include or require elsewhere,
> so i want to be able to just:
>    require 'util.php';
> and have the PHP include path "do the right thing."  right now, it's
> ugly, as in, having PHP files that do:
>    require '../../proj/util.php';
> meaning every single file that wants to include another proj-related
> file has to know its relative position in the proj hierarchy.   barf.

I used to use auto detecting absolute paths but switched to relative 
paths when a client decided to switch  his hosting company, I *think* it 
was GoDaddy. They required relative paths when using the shared SSL 
server or the browser would throw errors.

It is really not bad as long a you keep your directory structure 
consistent. I have never received a bug report because of an include 
path issue. It also does not matter where the root directory of the app 
is located since everything is relative anyway. There is also no need 
for the user to configure anything because of the relative paths.

So I just set $include = './include/abc/def/' at the top of the 
executing php page and use it further down. You make $include a global 
or simply pass $include to functions or classes when they need to 
include files. I use the later approach.

As I said I never had a problem with it, it just requires consistency.

-- 
John
Eine der erstaunlichsten Erscheinungen ist, daß man sich einbildet,
von abhängigen Menschen unabhängige Meinungen erwarten zu dürfen.
[Sigmund Graff]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.