yii\di\ServiceLocator::get PHP Метод

get() публичный Метод

Returns the component instance with the specified ID.
См. также: has()
См. также: set()
public get ( string $id, boolean $throwException = true ) : object | null
$id string component ID (e.g. `db`).
$throwException boolean whether to throw an exception if `$id` is not registered with the locator before.
Результат object | null the component of the specified ID. If `$throwException` is false and `$id` is not registered before, null will be returned.
    public function get($id, $throwException = true)
    {
        if (isset($this->_components[$id])) {
            return $this->_components[$id];
        }
        if (isset($this->_definitions[$id])) {
            $definition = $this->_definitions[$id];
            if (is_object($definition) && !$definition instanceof Closure) {
                return $this->_components[$id] = $definition;
            } else {
                return $this->_components[$id] = Yii::createObject($definition);
            }
        } elseif ($throwException) {
            throw new InvalidConfigException("Unknown component ID: {$id}");
        } else {
            return null;
        }
    }

Usage Example

Пример #1
0
 protected function runTasks()
 {
     foreach ($this->_tasks as $k => $id) {
         Console::output(sprintf('Running task "%s" (%d/%d)', $id, $k + 1, count($this->_tasks)));
         $result = $this->_container->get("tasks.{$id}")->run($this->_container, $this);
         if ($result === false) {
             Console::error(sprintf('Task "%s" failed.', $id));
             return false;
         }
     }
     return true;
 }
All Usage Examples Of yii\di\ServiceLocator::get