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 6047797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:23:29+00:00 2026-05-23T07:23:29+00:00

An Observer Design Pattern is the solution to loosely coupling objects so they can

  • 0

An Observer Design Pattern is the solution to loosely coupling objects so they can work together. In PHP you can easily implement this using just two classes.

Basically, you have a subject which is able to notify and update a list of observers of its state changes.

The problem I’m trying to solve is to know how to handler alerting the observers about different states of the object they are watching.

For example, lets say we have a file upload class to which we attach a logging class, websockets class, and a image resize class. Each of these classes that are watching want to know about different events in the upload process.

This file upload class might have three places where it needs to notify the classes listening that something has happend.

  • Error With Upload (alert logging class)
  • Upload success (alert websockets class)
  • Upload success and is image file (alert image resize class)

This is a very basic example, but how do you handle multiple events that different observers may need to know about? Calling notifyObservers() alone wouldn’t be enough since each observer needs to know what it is being notified about.

One thought is that I could state with the call what type of event is being observed:

$this->notifyObservers('upload.error', this);

However, that would mean I would have to add custom switching to the observers themselves to know how to handle different events.

function observe($type, $object)
{
    if($type === 'upload.error') $this->dosomething();
    elseif($type === 'something.else') $this->otherthing();
    ...etc...
}

I find that very ugly as it starts to couple the observers back to the class they are observing.

Then again, if I just notify Observers without passing any information about what event just happens – they have to guess themselves what is going on which means more if() checks.

  • 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-05-23T07:23:30+00:00Added an answer on May 23, 2026 at 7:23 am

    The observers aren’t actually coupled to the class they are observing. The connection between the observer’s handler and the observed object is made using literal string values (e.g. `upload.error’), which means that:

    1. If you want to observe a specific object, you have to know from beforehand the names of the events it will publishing; this is the “coupling” that you don’t like.
    2. On the other hand, if you are interested in a specific event only, you can observe any type of object for that event without having any knowledge about that object.

    Item 2 above is a benefit that you care about, but what to do about item 1?

    If you think about it, there needs to be some way to differentiate between callbacks to the same observer if they represent different events taking place. These “identifiers”, no matter what form they take, need to be packaged either into the observed object or be a part of the observer library code.

    In the first instance (inside observed object) you would probably need a way for observers to query “do you ever publish event X?” before starting to observe a target for that event. The target can answer this question just fine. This leaves a bitter taste of coupling, but if you want any object to observe any other, and you have no idea what you will be observing beforehand, I don’t think you can do any better.

    In the second approach, you would have a number of well-known events defined (as const inside a class?) in your library. Presumably such a list of events can be made because the library tackles a concrete application domain, and that domain offers obvious choices for the events. Then, classes both internal to your library (which would end up being observed) and external to it (the observers which plug into the framework) would use these identifiers to differentiate between events. Many callback-based APIs (such as Win32) use an approach practically identical to this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two questions: I'm trying to implement an Observer design pattern in Python.
In the Observer Design Pattern , the subject notifies all observers by calling the
Is the observer design pattern already defined in STL (Like the java.util.Observer and java.util.Observable
It seems that the observer design pattern is in built in C# through its
In reading about the Observer design pattern, I noticed that it is implemented using
It seems to me that the Observer design pattern as described in GoF is
I found different implementations of the observer pattern in Delphi, like: Sourcemaking Design Patterns
I am trying to implement observer pattern for my project. The LeaveFields class extends
Using MVC with an observer pattern, if a user action requires polling a device
I am having problems with implementing Observer pattern in my project. The project has

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.