Hi,
This isn’t really a request, just a little suggestion/idea regarding the event system, but it felt like the appropriate place to post.
I’m currently working on a little project of mine, which happens not to be Yii-related (cause it’s command-line and w/out database…) but where I wanted to have an event system, which turned out to be similar/inspired by Yii’s implementation. Of the little differences I made, here’s one that I thought might turn useful if implemented in Yii as well, possibly.
Right now, the onXXX methods are well as raiseEvent require a param $event, the event to be raised. Why not make it optional? That is, if it’s not specified then raiseEvent creates it automatically. This is simply to save a few characters to be typed, when we don’t need to do anything specific other than raising a CEvent. E.g:
// instead of:
$this->onFoobar(new CEvent($this));
// we'd do this:
$this->onFoobar();
Obviously this only applies when raising an event on the current object ($this) so that raiseEvent can set the sender, but it might (should?) be the case most of the times, no? (Besides, this would only be an addition, leaving the current syntax fully supported of course.)
Also, you could even have raiseEvent return the event when done (and of course, all the onXXX methods would need to be updated as well), so if one needs to raise an event, and then needs to use it, e.g. to check if it’s been handled or not, you could do:
$event = $this->onFoobar();
// or, to only check if it's been handled or not:
if (!$this->onFoobar()->handled)
Additionally, you could even accept an array as parameter $event, to automatically set some properties. This would allow something like this:
if (!$this->onFoobar(array('foo' => 'bar', 'answer' => 42)->handled)
// or maybe even:
if (!$object->onFoobar(array('sender' => $this, 'foo' => 'bar', 'answer' => 42)->handled)
It seems from the code that right now one can use anything as $event (CEvent or a child class, but also anything else, including int, string, etc) while the documentation states it must be a CEvent, so I’m not sure if doing so would break anything or not. Shouldn’t if the rules from the documentation have been followed though, right?
Anyways, just an idea
-pinpin