3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php trait iaSplSubject { protected $iaSubjectTag = ''; protected $iaSubjectEvent = ''; protected $iaObserverQueue = []; public function get_iaSubjectTag() {return $this->iaSubjectTag;} public function set_iaSubjectTag($subjectTag) {$this->iaSubjectTag = $subjectTag;} public function get_iaSubjectEvent() {return $this->iaSubjectEvent;} public function set_iaSubjectEvent($subjectEvent) {$this->iaSubjectEvent->iaSubjectEvent;} public function attach(SplObserver $SplObserver) { $this->iaObserverQueue[spl_object_hash($SplObserver)] = [ count($this->iaObserverQueue), $SplObserver]; if(method_exists($SplObserver, 'get_priority')) { uasort($this->iaObserverQueue, array($this, 'iaObserverQueueCmp')); } } protected function iaObserverQueueCmp($a, $b) { if(method_exists($a[1], 'get_priority') && method_exists($b[1], 'get_priority') && $a[1]->get_priority() != $b[1]->get_priority()) { return $a->get_priority() - $b->get_priority(); } return $a[0] - $b[0]; } public function detach(SplObserver $SplObserver) { unset($this->iaObserverQueue[spl_object_hash($SplObserver)]); } public function notifyEvent($subjectEvent) { $this->set_iaSubjectEvent($subjectEvent); $this->notify(); $this->iaSubjectEvent = ''; } public function notify() { foreach($this->iaObserverQueue as $notify) { $notify[1]->update($this); } } } trait iaSplObserver { protected $iaSplObserverPriority = 1000; public function set_iaSplObserverPriority($priority) {$this->iaSplObserverPriority = $priority;} public function get_iaSplObserverPriority() {return $this->iaSplObserverPriority;} public function iaSplSubject_attach(SplSubject $SplSubject, $priority = 1000) { $this->set_iaSplObserverPriority($priority); $SplSubject->attach($this); } public function iaSplSubject_getTag(SplSubject $SplSubject) { return method_exists($SplSubject,'get_iaSubjectTag') ? $SplSubject->get_iaSubjectTag() : ''; } public function iaSplSubject_getEvent(SplSubject $SplSubject) { return method_exists($SplSubject,'get_iaSubjectEvent') ? $SplSubject->get_iaSubjectEvent() : ''; } } class veo implements SplObserver { use iaSplObserver; private $rata=1; public function dime() {echo "dice ".$this->rata;} public function update(SplSubject $SplSubject) { // $tag = $this->SplSubject_getTag(SplSubject $SplSubject); // $event = $this->$SplSubject->get_iaSubjectEvent(); } } class aviso implements SplSubject { use iaSplSubject; } $t = new veo(); $t->dime(); $a = new aviso();
based on 5tKrc
Output for 5.6.0 - 5.6.30, hhvm-3.18.5 - 3.22.0, 7.0.0 - 7.3.0alpha1
dice 1