Yii events.

I am finding extreme difficulty in understanding "EVENTS" IN Yii.

I can not comprehend what is the role of instance of CEvent.

For example,Let us have two anonymous functions.




function greetOne($name) {echo "hello ".$name;}

function greetTwo($name) {echo "hello ".$name;}



Let us have an object inherited from CComponent,$component.

Let us assume we have an event,"onMeeting".

Now we will attach the two functions to the event.




$component->onMeeting="greetOne";

$component->onMeeting="greetTwo";



My question is how to declare the CEvent, so that we can pass parameters

to both the anonymous functions.For example I want to pass "Jack" to greetOne

and "Jill" to greetTwo as parameters.

The following is not working.




$component->raiseEvent('onMeeting',new CEvent($this,'Jack','Jill'));



More over I am not able to understand the purposes of "sender" and "params"

declared in CEvent.Where else they are called.

I am desperate in understanding the "events in Yii",as it is a key aspect of it.

Say you have defined the following events in one of your components:




    public function onBeforeInit($event)

    {

        $this->raiseEvent('onBeforeInit', $event);

    }


    public function onAfterInit($event)

    {

        $this->raiseEvent('onAfterInit', $event);

    }



Next, you need to define the event handlers for these two events, something like:




    public function init()

    {

        if($this->hasEventHandler('onBeforeInit'))

        {

            $event=new CEvent($this);// $this becomes the sender

            $event->params['key']='value';

            $this->onBeforeInit($event);

        }

        

        parent::init();

        

        if($this->hasEventHandler('onAfterInit'))

        {

            $event=new CEvent($this);

            $event->params['key2']='value2';

            $this->onAfterInit($event);

        }

    }



Next, from another component, you can :




class SomeComponent extends CApplicationComponent(){


   public function init()

   {

      parent::init();

      Yii::app()->theComponentWhichDefinedTheEvents->attachEventHandler('onBeforeInit',array($this,'beforeInit'));

   }


   public function beforeInit($event)

   {

      if($event->params['key']=='value')

      {

         // do something...

      }

   }


}



At least this is my understanding about events/event handlers, and yes, i agree with you that those are somehow hard to understand.

However, take a look at my signature and download the ExtensionLoader extension, you will see that it defines some custom events and event handlers and also some behaviors that make use of those events.

I realy thank you for a commited response.

I have realy got some insight after going through your post.

I have understood the following;

1.You can not just like that attach any global functions or object

methods accepting parameters to any events.

2.Instead they should be tailor-made to accept the instance of CEvent as

its parameter.The property "params" of CEvent shoud be used inside the functions

for customization.

[color="#FF0000"]The following is not going to work:[/color]




public function onMeeting($event) {

$this->raiseEvent('onMeeting',$event)

}


$event=new CEvent($this,'Jhon');


function greet($name) {

echo "hello ".$name;

}


$component->onMeeting="greet";

$component->onMeeting($event);



[color="#008000"]The following is going to work:[/color]




public function onMeeting($event) {

$this->raiseEvent('onMeeting',$event)

}


$event=new CEvent($this,'Jhon');


function greet($event) {

echo "hello ".$event->params;

}


$component->onMeeting="greet";

$component->onMeeting($event);



"sender" is still elusive to my comprehension.

Thank you.