views:

310

answers:

7

I have read that when including a php file that using a absolute paths has a faster processing time than relative paths.

What would you suggest to use?

include("inlcudes/myscript.php");

or

include("/home/ftpuser/public_html/includes/myscript.php");

or even

set_include_path("/home/ftpuser/public_html/includes");
include("myscript.php");

Or is it something that I really shouldn't worry about?

Thanks in advance!

A: 

when not using an absolute path, php tries to find the file in all include paths until it finds a match.

as many include paths can be added as you like, so this could , in rare cases, cause the script to be slow.
If you are including many files (for instance to initialize a framework) using absolute paths could speed up the script a little...

I think it could also cause complications when the same relative path/filename pare occur multiple times on the filesystem, and thus php selects the first occurence, when you might need another occurence

Nicky De Maeyer
A: 

It would be good for you to test all methods by checking the time it takes to execute each, personally I have never worried about it and just used relative paths.

I guess absolute paths would be slightly faster, it might be worth wondering what happens in an error, will it spit out your full file path to the users screen (obviously turn error_reporting off) and will this cause a security risk?

ILMV
It spits full file paths out either way as far as I'm aware. It's the only way to be sure just which `index.php` you mean.
Matthew Scharley
+1  A: 

Definitely don't hard-code your paths, like option two. A good alternative is:

define('BASE_DIR', '/home/ftpuser/public_html/includes');
include(BASE_DIR . '/myscript.php');
include(BASE_DIR . '/myscript2.php');
include(BASE_DIR . '/myscript3.php');
include(BASE_DIR . '/myscript4.php');

Considering you'll probably have somewhere between 5 and 50 includes (I'm guessing), I wouldn't really worry about it. Just go with relative paths. The include time difference won't even be noticable. If you're developing a big web application and will have hundreds, that might be another story...

brianreavis
+1  A: 

I tend to setup my include directories / libraries by setting the include path on the initialisation of my app.

set_include_path("/home/ftpuser/public_html/includes");
include("myscript.php");

The zend framework does something similar to load the library classes.

Andi
+4  A: 

I usually set a constant, either manually or like this:

define('ROOT', dirname(__FILE__));

Then do

require ROOT . '/include/file.php';
Greg
If you have `ROOT`, why not `set_include_path(get_include_path().PATH_SEPARATOR.ROOT)`?
chelmertz
Because then you're still searching the include paths - this way there's no searching involved.
Greg
It's a pretty simple/fast search if you place it first: `set_include_path(ROOT.PATH_SEPARATOR.get_include_path())`. Also, the maintainability could be compromised if there are alot of files to be included (elaborated my opinion more in an answer.)
chelmertz
+2  A: 

This is the best method for 99% of cases:

include(dirname(__FILE__)."/includes/myscript.php");

This ends up being an absolute path, which means it will ignore include_path, which is a known source of a large number of include related bugs in my experience...

Performance wise though, I doubt there's much difference between absolute and relative paths. This is a sort of micro optimisation that means nothing in the long run. There will generally only be 2-3 things in include_path unless you add more in. The two usual culprits are ./ and the path to wherever pear is installed.

Matthew Scharley
A: 

The most important thing is to arrange the include paths so that the largest amount of require/include-calls are trapped in the first mentioned path when not including a file via an absolute path in the first place.

Relying on including everything via an absolute path is hard to maintain because changing the path of your library means individually changing all those files refering to it instead of changing the include path in one place.

chelmertz