Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Daily Dose of Absurdity

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #166730
    __
    Participant

    I’m working on a version of the SplObserver+SplSubject interface that supports event-driven dispatching of notifications.

    This is what happens when you’ve been working for eight hours and then start writing code for three different classes that all have to work with each other (at the same time) and all implement the ArrayAccess interface.

    /**
     * maintains list of observers for this subject.
     *
     * @param object $observer          an observer
     * @param string $event             the event to attach to/ detach from observer
     * @param bool   $attach            attach the event (rather than detach it)?
     * @return object|void              the list of observers if no $observer provided
     */
    protected function _observers( \SplObserver $observer=null,$event="*",$attach=true ){
        static $_observers;
        if( ! $_observers ){ $_observers = new SplObjectStorage; }
        // getter mode
        if( ! $observer ){ return $_observers; }
        // setter mode
        if( ! $_observers->offsetExists( $observer ) ){
            if( ! $attach ){ return; }
            $_observers->offsetSet( $observer,Arrays::makeSet( $event ) );
            return;
        }
        $method = $attach?
            "offsetSet":
            "offsetUnset";
        $_observers->offsetGet( $observer )->$method( $event );
    }
    

    Just to reassure everyone, this is an internal method that normal users will never deal with.

    edit

    There’s no question. Just sharing a little bit of exasperation. Moderators, maybe I should have put this in “Other Discussions”…? sorry.

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.