Page 1 of 1
oo php with multiple directories
Posted: Sun May 31, 2009 1:47 am
by kcormier
Howdy all.
Basically I have an object oriented php application that I'd like to span multiple folders.
There is one directory though that houses all my classes that I'd like to be able to access from anywhere.
I'd like to avoid relying on .htaccess files and the like as not all web servers support it, right? (at least i've found one that doesn't...).
I want the application i'm developing to support any web server that runs php, and also to support relative paths. So someone could access it at example.com or my.example.com or example.com/myapp/
I'm not sure of the terminology to describe that but I hope you understand what I mean. If anyone wants to also throw in the terminology I'm looking to learn as much as I possibly can!
My solution right now is to have a custom class loader & such for each directory. Is this really the best way to do it?
-Kevin
Re: oo php with multiple directories
Posted: Sun May 31, 2009 5:20 am
by Christopher
I don't know if you need a loader. You may be able to just load classes with relative pages (e.g. classes/MyClass.php). You need to tell us a little more about your classes and directory structure.
Re: oo php with multiple directories
Posted: Sun May 31, 2009 11:57 am
by kcormier
it might look something like
/
index.php
header.php
admin/
another_index.php
another_page.php
user_page/
yet_another_index.php
yet_another_page.php
classes/
class_1.php
class_2.php
class_3.php
What happens is I need to be able to reference all the classes from any php file. The simple way would be to modify the include_path directive in the .htaccess file to include the classes folder but they can't ensure it'll be running on a server that supports .htaccess. That and I won't know the absolute path for the htaccess file. Can you use relative paths there? I don't think so.
What i'm doing right now is I create a header.php file in each directory. That script sets the relative path to the root of the web application directory.
So, for example:
at the root $base_path = "";
in any first level directory $base_path = "../";
second level, $base_path = "../../";
Then it includes the main header.php file based off of what basepath is set to. This header then creates a custom class loader based off of what $base_path is set to.
Re: oo php with multiple directories
Posted: Sun May 31, 2009 12:18 pm
by McInfo
You might be able to come up with something creative with these:
PHP Manual:
Edit: This post was recovered from search engine cache.
Re: oo php with multiple directories
Posted: Sun May 31, 2009 1:32 pm
by Christopher
Yes, add the path to your 'classes' or 'includes' directory to the include_path.
Re: oo php with multiple directories
Posted: Sun May 31, 2009 4:34 pm
by kcormier
That's what I've done, usually through .htaccess files. Basically I'm looking to see if there's a creative way to figure out what the path to the classes folder is though at runtime. The only way I can think to do it is to hard code the relative path into each file because of the folder structure.
Re: oo php with multiple directories
Posted: Sun May 31, 2009 9:52 pm
by Christopher
You can use dirname() on one of the superglobals that give the path to the current script. Check phpinfo() to get the path to the current script.
Re: oo php with multiple directories
Posted: Wed Jun 03, 2009 12:04 pm
by kcormier
thanks for the advice

. It still leaves me writing a different header for each level in the directory, but it gives me some extra flexibility, allowing me to reference stuff absolutely if the need ever arises. Thanks!
-Kevin