SplSubject
在线手册:中文 英文
PHP手册

SplSubject::attach

(PHP 5 >= 5.1.0)

SplSubject::attachAttach an SplObserver

说明

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

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

Warning

本函数还未编写文档,仅有参数列表。

参数

observer

The SplObserver to attach.

返回值

没有返回值。


SplSubject
在线手册:中文 英文
PHP手册
PHP手册 - N: Attach an SplObserver

用户评论:

workspace dot development at yahoo dot com (02-Dec-2011 04:46)

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[] = max( debug_backtrace( ) );
     }
}

class
YourFramework {

     public function
launchPlatform() {

         
//could not let user to launch application twice!
         
Observer::wasTriggeredOnce();
     }
}
//cause is static
Observer::hereIsnoNeedToInstantiateAgain();
?>

goran (17-Oct-2011 08:54)

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