2014-03-28 25 views

回答

3

正如米歇爾說,你應該在你的方法

function charge(){ 
    //Perform charge related actions 
    $this->onCharge(new CEvent($this)); 
    //... maybe some code over here after raising the event 
} 

public function onCharge($event){ 
    $this->raiseEvent('onCharge', $event); 
} 

使用事件現在,你必須抓住引發的事件。所以在這個部分,你會宣佈哪些類和mehod需要捕捉它(例如在init法)

//OtherClass need to catch the the charge event 
$callback = array(new OtherClass,'afterCharge') 
$component->onCharge = $callback; 

所以這種方法在執行charge()OtherClassafterCharge將被調用。

順便說一句,該方法afterCharge必須具有以下簽名

function afterCharge($event) { 
} 

來源:

+0

謝謝達爾kheir :-) – hoangk

+0

另外,我有一個問題:$ component是全局變量。非常感謝 – hoangk

+0

不,這是一個拋出事件的類,所以你可以用$替換它 – darkheir