Inlude Path

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
ndnow
Forum Newbie
Posts: 6
Joined: Mon May 09, 2005 11:51 am

Inlude Path

Post by ndnow »

I was just wondering how to handle the include path properly within a code library. I am writing a library
to use on all my programs that will handle database usage, session handling and whatever else I will need.
I've been fighting in my mind over how to handle the include path with PHP and have come to no conclusion
that I like.

I was wondering which approach I should take. For instance I will have a child class that will include the parent
class within the child class but can be instantiated from anywhere in the program. The only sound solution I
can find is to set the include path on the server but I dont necessarily like this idea because not all servers
will allow for this. Are their any suggestions you guys have on this subject or am I just stuck setting the include
path?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

well you can set the include path like this of course:

Code: Select all

set_include_path(get_include_path() . PATH_SEPARATOR . YOUR_PATH_HERE);
But I would look into something like a service locator...

viewtopic.php?t=51794&highlight=service+locator
viewtopic.php?t=51873&highlight=service+locator

Another thing to look at (if php5) is __autoload:

viewtopic.php?t=52943&highlight=autoload
ndnow
Forum Newbie
Posts: 6
Joined: Mon May 09, 2005 11:51 am

Post by ndnow »

yeah those are the kind of idea's I was looking for I really dont want to set the include path I would like to stray from that. Thanks a lot for the links
much much appreciated.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

what's the problem with setting the include path?
ndnow
Forum Newbie
Posts: 6
Joined: Mon May 09, 2005 11:51 am

Post by ndnow »

From my programming perspective it just doesn't seem very organized having to set something before use. But Im weighing all my options
and will most likely use it in the end but would like to know what options are out there and explore them.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

...?? that's what it's there for... ?? :?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I don't really understand what thoughts are going through your mind ndnow but I'll tell you how I manage including in my library in the hope that it might be relevent to your problem.

Firstly its pretty much a given that a library will have to be installed in the include path. So what I have done is to put the bulk of my library code in a folder that is supposed to go in the include path with a couple of files outside of that the will go directly in the include path.

Code: Select all

include_path/OF.php
include_path/OF_Autoload.php
include_path/OF/*.php (loads of files)
This means that the user of my library can:

Code: Select all

require_once 'OF_Autoload.php';
and forget about including completely or if they like:

Code: Select all

require_once 'OF.php';

function __autoload($class) {
	if (OF::isDecendentClass($class)) {
		OF::loadClass($class);
	} else {
		// any existing autoload code you had
	}
}
..or load classes manually as they require...

Code: Select all

require_once 'OF.php';

// these are all equivalent
OF::loadClass('OF_Select');
OF::loadClass('Select');
require_once 'OF/Select.php';
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

ndnow wrote:From my programming perspective it just doesn't seem very organized having to set something before use. But Im weighing all my options
and will most likely use it in the end but would like to know what options are out there and explore them.
lol...

Programming is all about properly initilizing something before using it...

Even in OOP a ctor() is called before using an object :lol:

Why would that seem impractical?
ndnow
Forum Newbie
Posts: 6
Joined: Mon May 09, 2005 11:51 am

Post by ndnow »

ole wrote:I don't really understand what thoughts are going through your mind ndnow but I'll tell you how I manage including in my library in the hope that it might be relevent to your problem.

Firstly its pretty much a given that a library will have to be installed in the include path. So what I have done is to put the bulk of my library code in a folder that is supposed to go in the include path with a couple of files outside of that the will go directly in the include path.

Code: Select all

include_path/OF.php
include_path/OF_Autoload.php
include_path/OF/*.php (loads of files)
This means that the user of my library can:

Code: Select all

require_once 'OF_Autoload.php';
and forget about including completely or if they like:

Code: Select all

require_once 'OF.php';

function __autoload($class) {
	if (OF::isDecendentClass($class)) {
		OF::loadClass($class);
	} else {
		// any existing autoload code you had
	}
}
..or load classes manually as they require...

Code: Select all

require_once 'OF.php';

// these are all equivalent
OF::loadClass('OF_Select');
OF::loadClass('Select');
require_once 'OF/Select.php';
Thanks Ole that is very helpfull.
Post Reply