Jump to content

classes and includes


davestewart

Recommended Posts

Hi there,

I have a question about using includes within classes, and the best way to handle the issue with relative file paths. I'm quite new at classes in PHP, so excuse any seemingly ignorant pre-suppositions or omissions!

 

Basically, I like to keep all my common user-defined functions in a folder like this: "/php/functions/". This is great for procedural files, but I'm having issues with classes. As includes seem to be relative to the main php file, any includes in my class file that have "../../lib/functions" in them fail. However, if I create a new child folder and contain my functions there, the relative include is fine.

 

To summarise:

 

// MAIN PHP FILE: "/app/folder/main.php"
include "../../lib/classes/Test.class.php";

// Test.class.php (FAILS):
include "../functions/myFunction.func.php"

// Test.class.php (SUCCEEDS):
include "functions/myFunction.func.php"

 

I'm getting somewhat lost by splitting everything up anyway, so it seems that the payoff of having everything within the one directory is good for several reasons:

 

1 - it works!

2 - all class code is kept within the one directory

 

I just want to get this straight before I carry on. Is this correct!?

 

Thanks so much for your time,

Dave

Link to comment
https://forums.phpfreaks.com/topic/37162-classes-and-includes/
Share on other sites

Use your super globals.

 

$_SERVER['DOCUMENT_ROOT']

 

This will never change and will always point to your root directory. Echo it out to see where it points and then change the location for each file as needed. Example:

 

require($_SERVER['DOCUMENT_ROOT'].'/includes/myfunctions.inc.php');

 

Link to comment
https://forums.phpfreaks.com/topic/37162-classes-and-includes/#findComment-177558
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.