From: "Robert P. J. Day" on

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.

based on my experience with shell and makefile-based projects, my
preference would be that anyone who checks out a copy of the project
can check it out anywhere they want, then they need to set the single
shell environment variable, say PROJ_DIR, to the top-level directory
location, say:

$ export PROJ_DIR=/home/rpjday/php/things/proj

i would then make sure that env vars are available to PHP scripts
via /etc/php.ini:

variables_order = "EGPCS"
^

and all PHP scripts would start off with something like:

set_include_path(get_include_path() . PATH_SEPARATOR . getenv('PROJ_DIR'));

at that point (and correct me if i'm wrong), all subsequent includes
or requires would simply need to reflect the location of the file
relative to its PROJ_DIR location (barring any weird conflicts with
system files having the same name, of course).

does that sound about right? is there a standard way to do this?
that is, to have a sizable PHP hierarchy and allow any files within
that hierarchy to include others conveniently without having to
hardcode directory names.

requiring users to simply set a single env var has always worked for
me. is that what others do to solve this? thanks.

rday
--

========================================================================
Robert P. J. Day Waterloo, Ontario, CANADA

Linux Consulting, Training and Kernel Pedantry.

Web page: http://crashcourse.ca
Twitter: http://twitter.com/rpjday
========================================================================
From: John Black on
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]
From: John Black on
On 03/16/2010 08:50 PM, John Black wrote:
> So I just set $include = './include/abc/def/' at the top of the

correction, I set $include to the relative path of the include directory
and then use it like this:

$include = '../../include/';

require $include.'abc/file1.php';
require_once $include.'abc/def/file2.php';
require_once $include.'file3.php';

Still works the same way but since all my include files are in include
or some sub directory of it this works perfect.


--
John
Vorurteil ist das Kind von Ignoranz.
[William Hazlitt]
From: Ryan Sun on
On Tue, Mar 16, 2010 at 1:57 PM, Robert P. J. Day <rpjday(a)crashcourse.ca> wrote:
>
> and all PHP scripts would start off with something like:
>
> set_include_path(get_include_path() . PATH_SEPARATOR . getenv('PROJ_DIR'));
>

just utilize include_path directive in php.ini
From: Rene Veerman on
On Tue, Mar 16, 2010 at 9:48 PM, Ryan Sun <ryansun81(a)gmail.com> wrote:
> just utilize include_path directive in php.ini

yea, or via ini_set('include_path', ....);
 |  Next  |  Last
Pages: 1 2
Prev: $_FILE array being truncated
Next: Database vs. Array