I am about to create a collection of classes in php which are interrelated. Now I would like to include files using their absolute path, ie
If I were in
'/com/mysite/licences/Generator.class.php'
I would like to include files like this:
include '/com/mysite/lib/Utils.class.php'
I don't want to include like this, I think it is confusing and ugly
include '../licenses/Utils.class.php'
This works ok, unless 'com' is not in the root of public_html. I assume I will need some sort of prefix to append to each file LIBRARY_ROOT that hold the location of 'com' in the current filesystem. So it might look like this
include LIBRARY_ROOT.'com/mysite/lib/Utils.class.php'
What is the best way to do this?
-----------------MY SOLUTION----------------
UPDATE :
It looks like this solution didn't work either because the set_include_path('/'); appears to reset at the closing php tag ?> not the one in the including file.
This is what I did
Created a file called like the following in the root of the filesytem
com.mysite.setup.inc.php
the long name is in the Java format for package names, it is like this to help prevent naming conflicts. If I called it setup.php it is possible that another file will also be called setup.php in the root folder.
com.mysite.setup.inc.php contains the following:
<?php
/**
* Setup file for each class
*/
//Path
set_include_path('/');
//Other common setup
?>
This will always be in the root folder so all classes can call it like this:
include '/com.mysite.setup.inc.php';
If the library gets moved all you have to do is update com.mysite.setup.inc.php to reflect the changes.
What we get is a clean naming convention with no need to append ugly PREFIXES to every single include. We can now include like this in any class file.
include '/com.mysite.setup.inc.php';
include 'com/mysite/lib/Utils.class.php';
include 'com/mysite/lib/text_utilities/Text.class.php';
include 'com/mysite/encryption/Cipher.class.php';
class MyClass {.......