Creating a library of classes in php - how to handle the library root location?
<p>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</p>

<p>If I were in </p>

<pre><code>'/com/mysite/licences/Generator.class.php'
</code></pre>

<p>I would like to include files like this:</p>

<pre><code>include '/com/mysite/lib/Utils.class.php'
</code></pre>

<p>I <strong>don't</strong> want to include like this, I think it is confusing and ugly</p>

<pre><code>include '../licenses/Utils.class.php'
</code></pre>

<p>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</p>

<pre><code>include LIBRARY_ROOT.'com/mysite/lib/Utils.class.php'
</code></pre>

<p>What is the best way to do this?</p>

<h2>-----------------MY SOLUTION----------------</h2>

<h2>UPDATE :</h2>

<p>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.</p>

<hr>

<p>This is what I did</p>

<p>Created a file called like the following in the root of the filesytem</p>

<blockquote>
  <p>com.mysite.setup.inc.php</p>
</blockquote>

<p>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.</p>

<p>com.mysite.setup.inc.php contains the following:</p>

<pre><code>&lt;?php
/**
 * Setup file for each class
 */

//Path
set_include_path('/');

//Other common setup

?&gt;
</code></pre>

<p>This will always be in the root folder so all classes can call it like this:</p>

<pre><code>include '/com.mysite.setup.inc.php';
</code></pre>

<p>If the library gets moved all you have to do is update com.mysite.setup.inc.php to reflect the changes.</p>

<p>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.</p>

<pre><code>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 {.......
</code></pre>
