This question may be obvious to some, but I am just beginning to scratch the surface of multi-tier architecture with web applications, particularly with PHP. I have a library containing several objects and it is expanding quickly. I always try to think in terms of re-useability and break my projects down into small, reusable, and loosely coupled components so that I am not only creating the project but I am also creating the parts for future projects. I am trying to design a sort of package management system so that I can stop using the absolute paths within include statements for objects. I would like to have a single file containing includes to every class file within my library. Along with the includes, I want to add a class with a “singleton” pattern that will allow me to grab instances of every object that I have in the library on demand. I plan to create a static getter for each object that returns a reference to a new instance. Basically, something like this…
include('classa.php');
include('classb.php');
include('classc.php');
class pacman{
private static $instance_stack;
public static function GetPackage($strPackage){
if(!self::$instance_stack){
self::$instance_stack['classa']=new ClassA();
self::$instance_stack['classb']=new ClassB();
self::$instance_stack['classc']=new ClassC();
}
if(array_key_exists(strtolower($strPackage),self::$instance_stack)){
return self::$instance_stack[strtolower($strPackage)];
}
else{
return false;
}
}
}
Then if I want to get class a…
include('pacman.php');
$ClassA = pacman::GetPackage('classa');
I realize that this is a very rudimentary implementation. My example just covers the bare minimum. My problem isn’t implementing this; rather, it is a question of whether or not hundreds of uninstantiated classes from the includes will have a noticeable effect on performance. If so, does anyone know of a workaround? I can’t tell you how hard it is to keep track of the relative paths of hundreds of classes all sorted into directors by content.
You should try autoloading them:
This loads classes “on-demand”, reducing the overhead and need to have all files included up-front. More information here: http://php.net/autoload