Forum Moderators: coopster

Message Too Old, No Replies

require once failure

Possible depth issue, I'm not sure... =/

         

milocold

5:19 am on Feb 14, 2009 (gmt 0)

10+ Year Member



Hi Peeps,

So this issues got me...well, it's got me drinkin' more beer than usual. =)

Here's my dir structure:


LIBRARY
¦
¦---CLASSES
¦ ¦
¦ ¦---class1.php
¦ ¦---class2.php
¦
¦---CONFIGS
¦ ¦
¦ ¦---config1.php
¦
¦
CMS
¦---index.php

My index.php includes look like this:

require_once('../library/classes/class1.php')

My class1.php requires class2.php like so...

require_once('class2.php')

BUT, class2.php needs config1.php! How do I include that? I currently have this in my class2.php:

require_once('../configs/config1.php')

...that's throws up a fatal error claimimg file can't be found when I start to test my app from index.php, it DOES work fine when I start to test from class2.php. Now, if I move my CONFIGS dir to be a child dir of CLASSES everything works fine (adjusting the path in class2.php to "configs/config1.php" of course) but this takes away from our current frameworks structure. I don't think I wanna do that on a whim! Drunk or not... o.0

So far, through research, I discovered that early versions of php (2004/5) had depth issues when using the include/require functions which wouldn't allow for this sort of thing. Is this still an issue? Or, is there something else I'm missing? It just seems so simple...ARGH!...*cough, sneeze*

Thanks in advance,

M. Cold

coopster

5:40 pm on Feb 14, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I discovered that early versions of php (2004/5) had depth issues when using the include/require functions which wouldn't allow for this sort of thing. Is this still an issue?

It never was an issue. I don't know where you read that information but I would avoid that resource for any further education or enlightenment. You have been misled.

The best way to set your include paths is from a root. First, if at all possible, try to keep your database access methods, other classes, etc. out of your public document root, but on many shared servers this is not an option. If not, then at the very least, password protect the directory that contains these documents/scripts. For now, let's say your library folder is in your DOCUMENT_ROOT.

$root = $_SERVER['DOCUMENT_ROOT']; 
require_once "$root/library/classes/class1.php";
require_once "$root/library/classes/class2.php";
require_once "$root/library/configs/config1.php";

Better options are to set the include_path for your environment and then you don't have to use the "$root" variation demonstrated here.

There is a message in our library that demonstrates how to Control your included files [webmasterworld.com].

milocold

8:28 pm on Feb 14, 2009 (gmt 0)

10+ Year Member



Thank you very much Coopster for the info! =)