View Single Post
Old 06-04-2008, 07:04 PM   #4 (permalink)
delayedinsanity
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Oh! hahah, no your documentation doesn't suck, though yes it is non-existent. My eye is what sucks, I thought you were creating new objects then destroying them immediately after, I glazed over the fact that you were just unsetting the variable holding the class name, not the object itself. So, let me try this again... when ever this construct is called it finds the list of files you've already included, and loads a new object into existence for each of those files that is a class (*.class.php). Interesting way to go about it.

Have you heard of the registry pattern? Check this out: Registry pattern

I use a version of it that automagically includes the file if necessary, so instead of including all the necessary files at the top of my script, anytime I call my load() method, it checks to see if it's in memory, and if not, includes it. Ala,

PHP Code:
/**
 * Adds a new object instance of the specified class to the
 * static registry library, and returns a reference
 *
 * @access public
 * @static
 * @param  string $szModule The module the specified class belongs to
 * @param  string $szClass  The name of the class
 * @param  mixed  $mArgs    Arguments to be passed to the object, can be a string or an array
 * @return object Returns an instance of the specified object from the registry library
 */

static function load ($szModule$szClass$mArgs=null) {

    
$szFilename PATH_TO_LIB."{$szModule}.{$szClass}.php";
    
$szClass    "tuatara_{$szModule}_{$szClass}";

    if (!
file_exists($szFilename)) throw new Exception("Library file cannot be found.");

    if (!
class_exists($szClass)) include($szFilename);

    if (!
array_key_exists($szClassself::$_registry)) {
    
        if (
is_array($mArgs)) {
            
$pReflect = new ReflectionClass($szClass);
            
self::$_registry[$szClass] = $pReflect->newInstanceArgs($mArgs);
        } else {
            
self::$_registry[$szClass] = new $szClass($mArgs);
        }
    
    }

    return 
self::$_registry[$szClass];


Works beautifully.
-m
delayedinsanity is offline  
Reply With Quote