Location: PHPKode > scripts > Class Manager > class-manager/manual.html
<html>
<head><title>ClassManager manual</title></head>
<body>
<h1>Introduction</h1>
<p>This package deals with organizing and auto-including source files of PHP classes.</p>

<p>Many PHP applications suffer from the "include-require" mess. Source code is spread in multiple files. Simply including everything would waste resources, but specifying everything that's needed is too verbose. This is where PHP's __autoload function comes into play, but using it in itself is not particularly developer-friendly. ClassManager is an attempt to create a convenient solution to address this issue and provide an easy-to-overview pattern to organize source files.</p>

<h1>Usage</h1>

<p>Specify in XML configuration what classes do you plan to use, and define what files contain them. After this is done, including these files will be automatic and transparent to the application:</p>

<p>
<pre>
&lt;?php

require_once('ClassManager/ClassManager.php');
ClassManager::instance()->loadDefaultXml();

new TestClass1();
new TestClass2();
?>
</pre>
</p>

<p>An example configuration looks like this:</p>

<p>
<pre>
&lt;?xml version='1.0' encoding='UTF-8'?>
&lt;repositories>
	&lt;repository basePath= "testclasses/">
		&lt;classes>
			&lt;class name="TestClass1" path="TestClass1.php" />
			&lt;class name="TestClass2" path="TestClass2.php" />
		&lt;/classes>
	&lt;/repository>
&lt;/repositories>
</pre>
</p>

<p>
Its not a requirement to have each class in separate files, but its strongly recommended to maintain a better overview of the file / class structure. It also makes it possible to set repositories to perform auto lookups to further simplify the configuration:
</p>

<p>
<pre>
&lt;?xml version='1.0' encoding='UTF-8'?>
&lt;repositories>
	&lt;repository basePath= "testclasses/" autoLookup="true" recursive="true" />
&lt;/repositories>
</pre>
</p>

<p>
<i>Note: auto lookup is done by looking for {basePath}{classNameWithTemplate}, and can perform recursive lookup. The file template is {className}.php by default, can be changed per repository)</i>
</p>

<p>Any number of repositories can be defined. Any number of config XMLs can be loaded in addition to its default configuration. Its also possible to add additional classes on the fly by using the ClassManager's interface.</p>

<p>The ClassManager offers some additional features like config validation, file validation (by performing file exist check and require all), class name collision detection. <i>(Note: some of these features need classes defined in configuration)</i></p>

<p>Check out some of the possible <a href="usage.php">use cases</a>, and an <a href="classmanager/classpath.xml">example configuration</p>


<p>Performance note: </p>
<p><i>Altough XML configuration is easy to overview, its parsing results in a little extra overhead (~0.002 seconds for a big classpath xml with 50 defined items). This may not be acceptable in heavily loaded production environments. To get rid of this overhead, ClassManager offers a feature to generate PHP code from config XMLs that adds classes and lookups directly through ClassManager's interface.</i></p>


</body>
</html>
Return current item: Class Manager