Just finished studying the observer design pattern, YII2 everywhere use of things, records to summarize, perhaps the impression will be deeper
/** * Viewer Mode */class father{ Private $_observers = Array (); Public Function Register ($sub) {/* Register Observer */ $this->_observers[] = $sub; } Public Function trigger () {/ * External Unified Access */ if (!empty ($this->_observers)) { foreach ($this- >_observers as $observer) { $observer->update ();}}} }/** * The interface to be implemented by the viewer */interface observerable{Public function Update ();}//actual use of class Subscriber implements observerable{public function Update () { echo ' observers test '; }}/** * After any class, as long as the inheritance Father can use the Observer pattern */class Sun extends father{} $test = new S UN, $test->register (new Subscriber), $test->trigger ();
PHP design mode-Viewer mode