SplSubject::attach

(PHP 5 >= 5.1.0)

SplSubject::attachAttach an SplObserver

Description

abstract public void SplSubject::attach ( SplObserver $observer )

Attaches an SplObserver so that it can be notified of updates.

Warning

This function is currently not documented; only its argument list is available.

Parameters

observer

The SplObserver to attach.

Return Values

No value is returned.

Коментарии

Автор:
The most obvious storage type for observers (stored in attach()) is array. In order to be able to detach attached objects, you should be able to identify it inside observer storage.

I suggest you attach observers this way:

$this->_observers[spl_object_hash($observer)] = $observer;

so you can detach it later if you need:

unset($this->_observers[spl_object_hash($observer)]);
2011-10-17 03:54:45
http://php5.kiev.ua/manual/ru/splsubject.attach.html
i think that Observer pattern is always implemented as "static" to be called from any scope, for example:
<?php
class Observer {
     
//attach here the caller's scope
     
private static $stackTrace = array( );
     public static function 
wasTriggeredOnce() {
         
//...if is not in the stack, then:
         
self::$stackTrace[] = maxdebug_backtrace( ) );
     }
}

class 
YourFramework {

     public function 
launchPlatform() {

         
//could not let user to launch application twice!
         
Observer::wasTriggeredOnce();
     }
}
//cause is static
Observer::hereIsnoNeedToInstantiateAgain(); 
?>
2011-12-02 10:46:31
http://php5.kiev.ua/manual/ru/splsubject.attach.html

    Поддержать сайт на родительском проекте КГБ