. ======================================================================*/ /** * KjwUtil contains various static methods. * * @note Unused. */ class KjwUtil { /** * Get a singleton instance. * * You call your singleton class like this: * $theClassInstance = &KjwSingleton::getInstance("ClassName"[, optional arguments]); * @param $className The name of the class. If not included yet, * this should be the same as the filename. * @param ... Optional arguments to the constructor. * @note Make sure you use case sensitive names! PHPs case-insensitivity might cause problems. * @note You can't pass variables by reference, due to the way PHP4 func_get_args works. * If you want references, hand them to the instance afterwards. */ function &getSingleton($className) { static $instances = array(); // array of instance names assert(is_string($className)); if (!array_key_exists($className, $instances)) { // instance does not exist, so create it if (file_exists(dirname(__FILE__) . "/$className.php")) require_once(dirname(__FILE__) . "/$className.php"); // * call_user_func doesn't let me create new objects.., that's not cool // so, no call_user_func here // * func_get_args() returns the variables by value, instead of by reference // so, if you want to pass references, you're out of luck :( $a = &func_get_args(); switch (func_num_args() - 1) { case 0: $instances[$className] = new $className(); break; case 1: $instances[$className] = new $className(&$a[1]); break; case 2: $instances[$className] = new $className(&$a[1],&$a[2]); break; case 3: $instances[$className] = new $className(&$a[1],&$a[2],&$a[3]); break; case 4: $instances[$className] = new $className(&$a[1],&$a[2],&$a[3],&$a[4]); break; case 5: $instances[$className] = new $className(&$a[1],&$a[2],&$a[3],&$a[4],&$a[5]); break; case 6: $instances[$className] = new $className(&$a[1],&$a[2],&$a[3],&$a[4],&$a[5],&$a[6]); break; default: trigger_error('KjwSingleton: too many arguments for getInstance.', E_USER_ERROR); } } return $instances[$className]; } } ?>