add "isInitialized" property to CWidget


In many of the widget-type extensions that I develop I find myself repeating a similar piece of code that looks like this:

class SomeWidget extends CWidget {

private $_isInitialized = false;

public function init() {

  if ($all_conditions_met) {

    $this->_isInitialized = true



public function run() {

  if (!$this->_isInitialized) {

    // log, throw exception or otherwise explode on this - depending on your needed biz logic.

    // the main thing - abort execution of run() at the beginning, if CWidget determined 'not initialized'



I haven’t found something ready in CWidget that I can use. Is there?

If not, I think this is worth implementing. I haven’t thought really where this could be inserted. Are there filters for CWidgets? If so, this could certainly be in some default filter method in CWidget itself. This should be automatic as possible, preferrably without a need to call ‘parent::init()’ etc.

What do you think?

Almost a year after the original post, I get to the same needed pattern in some app component which I write.

In it, I’d like that the app component methods run only if some conditions are met when the init() method of the component run.

I can ‘feel’ that this pattern is generic and I suspect that the solution I suggested above is not the only option and I might have missed some other mechanism to achieve the same.

I would also add to the described solution an attribute named “nonInitReason” which is a string that will be outputted to the log in the example above, when run() method is run and $this->_isInitialized is evaluated to false. This way, the ‘problem’ and its cause will be adjacent in the log file.