An observer object registers
interest in an observerable object , and then when something happens to the observerable, the observer is
notified and can take appropriate action. In our case, this is shown in Figure 8.3.
Figure 8.3 The Observer pattern allows us to decouple the search indexing from the model data. This will allow us to easily add new
models to be searched or to change the way we index for searching.
Our observable classes are our models and so we want to allow observers to register themselves with the
models. The data we are interested in are instances of Zend_Db_Table_Row_Abstract, and so we??™ll create an
extension class called Places_Db_Table_Row_Observerable that will contain the functions that allow us to
register and notify observers. Listing 8.4 shows the class skeleton.
Listing 8.4: Places_Db_Table_Row class
class Places_Db_Table_Row_Observerable extends Zend_Db_Table_Row_Abstract
{
protected static $_observers = array(); A
public static function attachObserver($class)
{
if (!is_string($class) || !class_exists($class) 1
|| !is_callable(array($class, 'observeTableRow'))) { 1
return false; 1
} 1
if (!isset(self::$_observers[$class])) { 2
self::$_observers[$class] = true; 2
} 2
return true;
}
protected function notifyObservers($event)
{
if (!empty(self::$_observers)) {
foreach (array_keys(self::$_observers) as $observer) { 3
call_user_func(array($observer , 'observeTableRow'),
$event, $this);
}
}
parent::_postInsert();
Licensed to Menshu You
Pages:
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230