- 10/13/2006 */ class AppModel extends Model { /* Array of observers wanting to be notified when the model is saved. */ var $beforeSaveL = array(); /** * Override the beforeSave callback and notify our observers. * Remember that if this method doesn't return true, the model will be * tagged as invalid and fail to save. */ function beforeSave() { return $this->notifyObservers(); } /** * Dump the observsers (PHP 5). */ function __destruct() { unset($this->beforeSaveL); } /** * Notify our observers. */ function notifyObservers() { $valid = true; foreach($this->beforeSaveL as $observer) { //Using a flag like this allows the observers to invalidate //the model, should they need to. The observers must implement //the modelSaving(&$model) method. $valid = $valid && $observer->modelSaving($this); } return $valid; } /** * Register an observer to be notified during beforeSave(). * @param $observer The observer. */ function addObserver(&$observer) { array_push($this->beforeSaveL, $observer); } } ?>