Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:24:09+00:00 2026-06-15T18:24:09+00:00

The Standard PHP Library includes what some resources call a reference implementation of the

  • 0

The Standard PHP Library includes what some resources call a reference implementation of the Observer pattern, by way of the SplSubject and SplObserver classes. For the life of me, I can’t figure out how these are very useful with no way to pass actual events or any other information along with the notifications:

class MySubject implements SplSubject {
    protected $_observers = [];

    public function attach(SplObserver $observer) {
        $id = spl_object_hash($observer);
        $this->_observers[$id] = $observer;
    }

    public function detach(SplObserver $observer) {
        $id = spl_object_hash($observer);

        if (isset($this->_observers[$id])) {
            unset($this->_observers[$id]);
        }
    }

    public function notify() {
        foreach ($this->_observers as $observer) {
            $observer->update($this);
        }
    }
}

class MyObserver implements SplObserver {
    public function update(SplSubject $subject) {
        // something happened with $subject, but what
        // was it???
    }
}

$subject = new MySubject();
$observer = new MyObserver();

$subject->attach($observer);
$subject->notify();

It seems like these interfaces are pretty much useless for any real world problem. Can someone enlighten me?


Edit:

Here’s my biggest problem with the interface (although there are others):

public function update(SplSubject $subject, Event $event) { /* ... */ }

…nets the following fatal error:

PHP Fatal error:  Declaration of MyObserver::update() must be compatible with SplObserver::update(SplSubject $SplSubject)

Edit #2:

Making the additional parameters optional by giving them defaults prevents the fatal error and provides a way to pass context, making implementations worthwhile. I wasn’t previously aware of this, so this pretty much answers my question. The solution is to pass your own event/message data, and check for its existance inside SplObserver::update().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:24:10+00:00Added an answer on June 15, 2026 at 6:24 pm

    You can implement the update method with an optional parameter and still satisfy the SplSubject interface.

    class MyObserver implements SplObserver {
        public function update(SplSubject $subject, $eventData = null) {
            if (is_null($eventData))
                // carefull
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am Studying about PHP SPL - (Standard PHP Library), And i am having
My problem: require_once '/includes/aws-sdk-1.5.2/sdk.class.php'; My environment: I have a pretty standard PHP site that
This is a custom encryption library. I do not know much about PHP's standard
Is there an standard output library that knows that php outputs to html? For
Is there a way in Node.js to define a Standard Library, which is automatically
Is there any javascript plugin/library/script that will can transform a standard PHP to use
I would like to use the PSR-0 Standard way to autoload classes without requiring
I'm currently working with php standard library functions to sanitize string inputs for mysql_query
Is there a de-facto standard library for creating ASCII tables in PHP? I want
I'm examining the Standard PHP Library (SPL). I used only arrays before and just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.