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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:11:15+00:00 2026-05-14T00:11:15+00:00

I’m trying to get unique IDs for object instances in PHP 5+. The function,

  • 0

I’m trying to get unique IDs for object instances in PHP 5+.

The function, spl_object_hash() is available from PHP 5.2 but I’m wondering if there is a workaround for older PHP versions.

There are a couple of functions in the comments on php.net but they’re not working for me. The first (simplified):

function spl_object_hash($object){
    if (is_object($object)){
        return md5((string)$object);
        }
    return null;
    }

does not work with native objects (such as DOMDocument), and the second:

function spl_object_hash($object){
    if (is_object($object)){
        ob_start();
        var_dump($object);
        $dump = ob_get_contents();
        ob_end_clean();
        if (preg_match('/^object\(([a-z0-9_]+)\)\#(\d)+/i', $dump, $match)) {
            return md5($match[1] . $match[2]);
            }
        }
    return null;
    }

looks like it could be a major performance buster!

Does anybody have anything up their sleeve?

  • 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-14T00:11:16+00:00Added an answer on May 14, 2026 at 12:11 am

    I ran a couple of quick tests. I really think you’d be better off storing real callbacks in your bind() function using bind('evt_name', array($obj, 'callback_function')). If you absolutely want to go the spl_object_hash route, rather than storing references with the event bindings, you’re looking at something like this:

    A var_dump / extract and hash id implementation:

    function spl_object_hash_var_dump($object){
        if (is_object($object)){
            ob_start();
            var_dump($object);
            $dump = ob_get_contents();
            ob_end_clean();
            if (preg_match('/^object\(([a-z0-9_]+)\)\#(\d)+/i', $dump, $match)) {
                return md5($match[1] . $match[2]);
                }
            }
        return null;
    }
    

    A naive references implementation:

    function spl_object_dumb_references(&$object) {
        static $hashes;
    
        if (!isset($hashes)) $hashes = array();
    
        // find existing instance
        foreach ($hashes as $hash => $o) {
            if ($object === $o) return $hash;
        }
    
        $hash = md5(uniqid());
        while (array_key_exists($hash, $hashes)) {
            $hash = md5(uniqid());
        }
    
        $hashes[$hash] = $object;
        return $hash;
    }
    

    This one was basically 5-50x worse than the class-based reference function across the board, so it’s not worth worrying about.

    A store references by class implementation:

    function spl_object_hash_references(&$object) {
        static $hashes;
    
        if (!isset($hashes)) $hashes = array();
    
        $class_name = get_class($object);
        if (!array_key_exists($class_name, $hashes)) {
            $hashes[$class_name] = array();
        }
    
        // find existing instance
        foreach ($hashes[$class_name] as $hash => $o) {
            if ($object === $o) return $hash;
        }
    
        $hash = md5(uniqid($class_name));
        while (array_key_exists($hash, $hashes[$class_name])) {
            $hash = md5(uniqid($class_name));
        }
    
        $hashes[$class_name][$hash] = $object;
        return $hash;
    }
    

    And you end up with results that look like this. Summary: the class based references implementation performs best around n/50 classes–at its best, it manages to pull off 1/3 the performance of the var_dump based implementation, and it’s usually much worse.

    The var_dump implementation seems to be tolerable, though not ideal. But if you’re not doing too many of these lookups, it won’t be a bottleneck for you. Especially as a fallback for PHP < 5.2 boxen.

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

Sidebar

Ask A Question

Stats

  • Questions 441k
  • Answers 441k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In function setTimer it is decided weather the carousel moves… May 15, 2026 at 5:33 pm
  • Editorial Team
    Editorial Team added an answer Could it just be that you have @content.nodes(''//'') AS datey(d)… May 15, 2026 at 5:33 pm
  • Editorial Team
    Editorial Team added an answer Save yourself the trouble with Ankh and install TortoiseSVN. If… May 15, 2026 at 5:33 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.