From 9d43930d2daeda5321468da2c709e3917ecf5e3c Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Fri, 1 Apr 2011 07:51:22 +0000 Subject: [PATCH] Make sure the init callback is only called once. --- flight/Flight.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/flight/Flight.php b/flight/Flight.php index 8c4e36f..4dc4da5 100644 --- a/flight/Flight.php +++ b/flight/Flight.php @@ -102,10 +102,12 @@ class Flight { /** * Loads a registered class. * - * @param string $class Class name - * @param array $method List of methods to load + * @param string $name Method name + * @param bool $shared Shared instance */ public static function load($name, $shared = true) { + static $loaded = array(); + if (isset(self::$classes[$name])) { list($class, $params, $callback) = self::$classes[$name]; @@ -113,14 +115,19 @@ class Flight { self::getInstance($class, $params) : self::getClass($class, $params); - if ($callback) self::execute($callback, $ref = array($obj)); + if (!$shared || !isset($loaded[$name])) { + if ($callback) self::execute($callback, $ref = array($obj)); + $loaded[$name] = true; + } return $obj; } + $class = ucfirst($name); + return ($shared) ? - self::getInstance(ucfirst($name)) : - self::getClass(ucfirst($name)); + self::getInstance($class) : + self::getClass($class); } /**