Question

I am building a shoping cart in Yii. Now I want to buid a function with name is afterCharged() the same as afterSave(). So how to can do it? Thank you in advance

Was it helpful?

Solution

As Michiel said you should use an event in your method

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);
}

Now you have to catch the raised event. So in this component you'll declare which class and which mehod need to catch it (for example in the init method)

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

So the method afterCharge of OtherClass will be called when charge() is executed.

By the way, the method afterCharge must have the following signature

function afterCharge($event) {
}

Sources:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top