Yii Framework Forum: add "isInitialized" property to CWidget - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

add "isInitialized" property to CWidget Rate Topic: -----

#1 User is offline   Boaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 367
  • Joined: 23-January 11

Posted 24 June 2012 - 06:08 AM

Hi,

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?
Therapeutic PHP sessions My LinkedIn Profile
0

#2 User is offline   Boaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 367
  • Joined: 23-January 11

Posted 09 May 2013 - 03:55 AM

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.
Therapeutic PHP sessions My LinkedIn Profile
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users