flight\core\Loader::load PHP Method

load() public method

Loads a registered class.
public load ( string $name, boolean $shared = true ) : object
$name string Method name
$shared boolean Shared instance
return object Class instance
    public function load($name, $shared = true)
    {
        $obj = null;
        if (isset($this->classes[$name])) {
            list($class, $params, $callback) = $this->classes[$name];
            $exists = isset($this->instances[$name]);
            if ($shared) {
                $obj = $exists ? $this->getInstance($name) : $this->newInstance($class, $params);
                if (!$exists) {
                    $this->instances[$name] = $obj;
                }
            } else {
                $obj = $this->newInstance($class, $params);
            }
            if ($callback && (!$shared || !$exists)) {
                $ref = array(&$obj);
                call_user_func_array($callback, $ref);
            }
        }
        return $obj;
    }

Usage Example

コード例 #1
0
 function testSharedInstance()
 {
     $this->loader->register('d', 'User');
     $user1 = $this->loader->load('d');
     $user2 = $this->loader->load('d');
     $user3 = $this->loader->load('d', false);
     $this->assertTrue($user1 === $user2);
     $this->assertTrue($user1 !== $user3);
 }
All Usage Examples Of flight\core\Loader::load