yiisoft/yii — pull request #1491 by creocoder (2012-09-27 15:55:15+00:00)
Title: CModule::setComponents() proper fix 2

For example my base admin controller without fix:

```php
user->loginUrl=array('admin/login');
Yii::app()->errorHandler->errorAction='admin/error';
}
}
```

What we see here? The components will be loaded regardless of whether they will be used in the code. This is not proper way. We should not encourage this. The right way:

```php
setComponents(array(
'user'=>array(
'loginUrl'=>array('admin/login'),
),
'errorHandler'=>array(
'errorAction'=>'admin/error',
),
));
}
}
```

Programmer does not have to worry about the components are loaded or not. If they change config at runtime level they know **what they do**. They will not try change `CHttpSession::autoStart` property at runtime because this property has no effect after component initialization.

With this fix we have proper workflow. For example:

```php
getComponent('errorHandler'); //CErrorHandler here

Yii::app()->setComponents(array(
'errorHandler'=>array(
'class'=>'AnotherErrorHandlerClass',
),
));

Yii::app()->getComponent('errorHandler'); //AnotherErrorHandlerClass here
Yii::app()->setComponent('errorHandler',null);
Yii::app()->getComponent('errorHandler'); //AnotherErrorHandlerClass here!
```

We need this fix because this features very needed for advanced usage. For example **Symfony Service Container** have all this features and ALWAYS have optimal lazy loading component workflow. Also any loaded component could be reconfigured at runtime level. Our framework need this features, because this is core features, it's needed by programmers interested in a perfect app memory consumption.