let's say we have:
Code:
index.php
[lib]
---[classes]
------class1.php
------class2.php
---config.php
To require [root]/lib/classes/class1.php from [root]/lib/config.php we can have:
1. require dirname(__FILE__).'/classes/class1.php';
2. require $_SERVER['DOCUMENT_ROOT'].'/lib/classes/class1.php';
3. require 'classes/class1.php';
First, 3 is not OK (worst case) because if our "config.php" is required by another file, from another folder, the path will not be valid anymore.
Let's say that in [root]/lib/classes/class1.php we have require '../config.php'; the final content of the file (class1.php + the include) will be:
Code:
<?php
require '../config.php';
// -- and this will produce
require 'classes/class1.php'; // (from our config.php)
// -- fatal error: we try to require [root]/lib/classes/classes/class1.php - not here!
1) and 2) use absolute server path so you cannot fail BUT if the website is moved into a folder (it's a strange case, but it can happen - the server is changed and the DOCUMENT_ROOT is not the same or you need a [beta] folder of the website), we'll have:
Code:
FOLDER
---index.php
------[lib]
---------[classes]
------------class1.php
------------class2.php
------config.php
In case (2), instead of
$_SERVER['DOCUMENT_ROOT'].'/lib/classes/class1.php';
we'll need
$_SERVER['DOCUMENT_ROOT'].'/FOLDER/lib/classes/class1.php';
In the (1)st case everything it's compatible.
No changes are needed. It's just a better practice that I would suggest.
Bookmarks