best way to set up an include path for a multi-level project?
best way to set up an include path for a multi-level project?
am 16.03.2010 18:57:10 von rpjday
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
============================================================ ============
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: best way to set up an include path for a multi-level project?
am 16.03.2010 20:50:11 von Skylinux
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]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: best way to set up an include path for a multi-level project?
am 16.03.2010 21:35:18 von Skylinux
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]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: best way to set up an include path for a multi-level
am 16.03.2010 21:48:39 von Ryan Sun
On Tue, Mar 16, 2010 at 1:57 PM, Robert P. J. Day 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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: best way to set up an include path for a multi-level
am 16.03.2010 23:34:20 von Rene Veerman
On Tue, Mar 16, 2010 at 9:48 PM, Ryan Sun wrote:
> just utilize include_path directive in php.ini
yea, or via ini_set('include_path', ....);
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: best way to set up an include path for a multi-levelproject?
am 17.03.2010 10:53:19 von rpjday
On Tue, 16 Mar 2010, John Black wrote:
> 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.
i'm not entirely sure what you're suggesting here, but i don't think
it will work for me as one of the things i want to do is set up a
"test" directory, inside which i might have subdirectories with more
test routines, so i can't guarantee *anyone's* position relative to
the top of the "proj" directory.
i might even want to write PHP scripts and place them quite some
distance from the "proj" directory but still want to include utility
scripts from the "proj" directory. based on how i've done this with
shell and makefile-based projects in the past, the easiest solution
seemed to be to simply demand that users set a single environment
variable identifying the location of the "proj" directory, and base
everything off of that by extending the include path. at that point,
everything falls into place and all include/require references work
relative to the top of the "proj" tree.
i think that's the direction i'm going to go, unless someone has a
compelling reason not to. 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
============================================================ ============
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: best way to set up an include path for a multi-level project?
am 17.03.2010 13:24:57 von Bob McConnell
From: Robert P. J. Day
> On Tue, 16 Mar 2010, John Black wrote:
>> 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 =3D './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.
>=20
> i'm not entirely sure what you're suggesting here, but i don't think
> it will work for me as one of the things i want to do is set up a
> "test" directory, inside which i might have subdirectories with more
> test routines, so i can't guarantee *anyone's* position relative to
> the top of the "proj" directory.
>=20
> i might even want to write PHP scripts and place them quite some
> distance from the "proj" directory but still want to include utility
> scripts from the "proj" directory. based on how i've done this with
> shell and makefile-based projects in the past, the easiest solution
> seemed to be to simply demand that users set a single environment
> variable identifying the location of the "proj" directory, and base
> everything off of that by extending the include path. at that point,
> everything falls into place and all include/require references work
> relative to the top of the "proj" tree.
>=20
> i think that's the direction i'm going to go, unless someone has a
> compelling reason not to. thanks.
IOW, you want to point into the first project's test directory from
other projects when you can't know the relative paths between those
projects?
I suspect you will have to manage that on a machine by machine basis,
unless you can convince the entire development team to create a common
directory structure that encompasses all projects.
Bob McConnell
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: best way to set up an include path for a multi-levelproject?
am 17.03.2010 15:28:13 von rpjday
On Wed, 17 Mar 2010, Bob McConnell wrote:
.... snip ...
> IOW, you want to point into the first project's test directory from
> other projects when you can't know the relative paths between those
> projects?
>
> I suspect you will have to manage that on a machine by machine
> basis, unless you can convince the entire development team to create
> a common directory structure that encompasses all projects.
let me emphasize that the layout of the entire "proj" directory will
be consistent across all users and all machines since it will
represent a single SVN checkout, so that's not an issue. of course,
anyone will be free to check it out anywhere they want but once they
do, its structure will be the same across all checkouts.
that's why i think the easiest solution is to define a single
environment variable (say, PROJ_DIR) that anyone can set depending on
where they do the checkout, the PHP code itself will (somehow)
initially consult that variable, and all inclusion from then on will
be based on that. piece of cake, no?
in fact, developers are free to do more than one checkout, work on
them independently, and change PROJ_DIR depending on which one they
want to pick up for their code.
does that more clearly explain what i think the right approach is?
rday
--
============================================================ ============
Robert P. J. Day Waterloo, Ontario, CANADA
Linux Consulting, Training and Kernel Pedantry.
Web page: http://crashcourse.ca
Twitter: http://twitter.com/rpjday
============================================================ ============
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: best way to set up an include path for a multi-levelproject?
am 17.03.2010 15:30:48 von rpjday
On Wed, 17 Mar 2010, Bob McConnell wrote:
> I suspect you will have to manage that on a machine by machine
> basis, unless you can convince the entire development team to create
> a common directory structure that encompasses all projects.
i'm not sure what you mean by the above. while that single project
(that others might want to take advantage of) will have to have a
uniform and consistent layout for everyone, i don't see that that will
enforce any sort of uniformity on *other* projects that might want to
take advantage of it. or am i misunderstanding you?
rday
--
============================================================ ============
Robert P. J. Day Waterloo, Ontario, CANADA
Linux Consulting, Training and Kernel Pedantry.
Web page: http://crashcourse.ca
Twitter: http://twitter.com/rpjday
============================================================ ============
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: best way to set up an include path for a multi-levelproject?
am 17.03.2010 16:01:37 von Teus Benschop
> let me emphasize that the layout of the entire "proj" directory will
> be consistent across all users and all machines since it will
> represent a single SVN checkout, so that's not an issue. of course,
> anyone will be free to check it out anywhere they want but once they
> do, its structure will be the same across all checkouts.
The problem of an easy include path also came up in our project. The
solution was to create a Bootstrap class, that sets the include path as
follows:
private function __construct() {
// Set the include path, where to look for included files.
// This is important so as to make pointing to the included files
much easier,
// and to avoid tortuous path references.
$this->bibledit_root_folder =3D dirname (dirname(__FILE__));
$include_path =3D get_include_path () . ":" .
$this->bibledit_root_folder;
set_include_path ($include_path);
ini_set('include_path', $include_path);=20
}=20
The central issue in the above is to use the __FILE__ variable as the
starting point for the include path. Since the bootstrap.php is in a
known location in the directory tree, we can always deduce a known
include path from that.
After that, each php file in each directory was set to include this
bootstrap.php, in various forms, depending on where this php file was
located in the directory tree:
require_once ("../bootstrap/bootstrap.php");
or:
require_once ("../../bootstrap/bootstrap.php");
or
require_once ("bootstrap/bootstrap.php");
That way the include path got set bootstrap.php. It works well.
Teus.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php