Inlude Path
Moderator: General Moderators
Inlude Path
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?
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?
well you can set the include path like this of course:
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
Code: Select all
set_include_path(get_include_path() . PATH_SEPARATOR . YOUR_PATH_HERE);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
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
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.
This means that the user of my library can:and forget about including completely or if they like:..or load classes manually as they require...
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)Code: Select all
require_once 'OF_Autoload.php';Code: Select all
require_once 'OF.php';
function __autoload($class) {
if (OF::isDecendentClass($class)) {
OF::loadClass($class);
} else {
// any existing autoload code you had
}
}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
lol...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.
Programming is all about properly initilizing something before using it...
Even in OOP a ctor() is called before using an object
Why would that seem impractical?
Thanks Ole that is very helpfull.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.This means that the user of my library can:Code: Select all
include_path/OF.php include_path/OF_Autoload.php include_path/OF/*.php (loads of files)and forget about including completely or if they like:Code: Select all
require_once 'OF_Autoload.php';..or load classes manually as they require...Code: Select all
require_once 'OF.php'; function __autoload($class) { if (OF::isDecendentClass($class)) { OF::loadClass($class); } else { // any existing autoload code you had } }Code: Select all
require_once 'OF.php'; // these are all equivalent OF::loadClass('OF_Select'); OF::loadClass('Select'); require_once 'OF/Select.php';