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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:55:52+00:00 2026-05-23T05:55:52+00:00

I was reading around about the Observer pattern, and found a dated article .

  • 0

I was reading around about the Observer pattern, and found a dated article. Having read through, I noticed an interesting mention in this paragraph:

The key methods to look at here are attach(), detach(), and notify(). attach() and detach() handle adding and removing observers. We use a little trick here. Objects quoted in string context resolve to a unique identifier (even if __toString() is defined). You can use this fact to build keys for an associative array. The notify() method cycles through all attached observers, calling update() on each. The UploadManager class calls notify() whenever it has something important to report on upload and on error, in this case.

Which references this example:

function attach(UploadObserver $obs) {
    $this->observers["$obs"] = $obs;
}

Now as mentioned, this article is dated. Casting objects to strings of course no longer works in this manner (I run 5.3.6 on my dev box, and push it for all client projects) but I’d like to achieve similar functionality. I can only think of (something like) this:

function attach(Observer $observer){
    $this->_observers[md5(serialize($observer))] = $observer;
}

function detach(Observer $observer){
    unset($this->_observers[md5(serialize($observer))]);
}

I’m curious, are there any other efficient ways to achieve this; creating a unique key from the object itself.

Caveat: I don’t want to get into defined keys, I use those often enough with other repositories and such, implementing __set($key, $value), etc.

Note: I understand MD5 isn’t ideal.


Update: Just found spl_object_hash, and I assume this is likely my best choice, however feel free to share your thoughts.

  • 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-23T05:55:53+00:00Added an answer on May 23, 2026 at 5:55 am

    You’re right that does not work that way any longer. You might want to use some other function instead: spl_object_hash()

    function attach(Observer $observer){
        $this->_observers[spl_object_hash($observer)] = $observer;
    }
    
    function detach(Observer $observer){
        unset($this->_observers[spl_object_hash($observer)]);
    }
    

    The serialization based approach has a design problem btw: I stops working when objects are identical by value or in other words if objects return the same serialized value, e.g. NULL. This is fully controllable by the objects themselves when they implement the Serializable interface.

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

Sidebar

Related Questions

Having read an existing post on stackoverflow and done some reading around on the
Just trying to get my head around Generics by reading this enlightening article by
I've been reading about the module pattern, but everything I read assumes that the
Reading this question I found this as (note the quotation marks) code to solve
Reading through this question on multi-threaded javascript, I was wondering if there would be
I'm new to C# and .NET, ,and have been reading around about it. I
I've been reading around the web about different alternatives to keeping track of users
I was reading around a lot about singleton. I am thinking about the dead
Been playing around with XCode for about 2 weeks now, and reading about MVC
I have been reading other questions and answers around this but I am not

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.