oo php with multiple directories

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kcormier
Forum Newbie
Posts: 11
Joined: Sun May 31, 2009 1:36 am

oo php with multiple directories

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: oo php with multiple directories

Post 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.
(#10850)
kcormier
Forum Newbie
Posts: 11
Joined: Sun May 31, 2009 1:36 am

Re: oo php with multiple directories

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: oo php with multiple directories

Post 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.
Last edited by McInfo on Tue Jun 15, 2010 9:59 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: oo php with multiple directories

Post by Christopher »

Yes, add the path to your 'classes' or 'includes' directory to the include_path.
(#10850)
kcormier
Forum Newbie
Posts: 11
Joined: Sun May 31, 2009 1:36 am

Re: oo php with multiple directories

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: oo php with multiple directories

Post 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.
(#10850)
kcormier
Forum Newbie
Posts: 11
Joined: Sun May 31, 2009 1:36 am

Re: oo php with multiple directories

Post 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
Post Reply