PHP 8.4.2 Released!

SplSubject::attach

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

SplSubject::attachAnexa um SplObserver

Descrição

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

Anexa um SplObserver para que ele possa ser notificado sobre atualizações.

Aviso

Esta função não está documentada; apenas a lista de argumentos está disponível.

Parâmetros

observer

O SplObserver para anexar.

Valor Retornado

Nenhum valor é retornado.

adicione uma nota

Notas Enviadas por Usuários (em inglês) 2 notes

up
2
workspace dot development at yahoo dot com
13 years ago
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();
?>
up
1
goran
13 years ago
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)]);
To Top