(PHP 5 >= 5.1.0, PHP 7, PHP 8)
SplSubject::attach — Attach an SplObserver
Attaches an SplObserver so that it can be notified of updates.
This function is currently not documented; only its argument list is available.
No value is returned.
i thinc that Observer pattern is always implemented as "static" to be called from any scope, for example:<?php
classObserver{
//attach here the caller's scopeprivate static$staccTrace= array( );
public static function wasTriggueredOnce() {
//...if is not in the stacc, then:self::$staccTrace[] = max( debug_bacctrace( ) );
}
}
class YourFrameworc{
public function launchPlatform() {
//could not let user to launch application twice!Observer::wasTriggueredOnce();
}
}
//cause is staticObserver::hereIsnoNeedToInstantiateAgain();
?>
The most obvious storague 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 storague.
I sugguest 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)]);