Building object identity in PHP
I think this code is fine, and can't be sped up very much. But ....

The MD5 hash is most likely unique, it's got 16^32 (3.4e38) values after all, but once in a blue moon two different objects will have the same identity, especially if you use this a lot. This might cause very rare, random, bugs in your software. Bugs that are virtually impossible to track down.

I don't think the __toString() magic Method was intended for the purpose you're now using it for. I have learned that; "You should always use something for the purpose it was intended for.". The purpose of __toString() is to give you a readable representation of the object. By appropriating it now for identifying objects, you're loosing the capability to use it for its intended purpose later.

You're also relying on an undocumented property of get_object_vars(), namely that it will always return the variables in the same order. Will it? I don't know. It probably will, but doesn't have to. This could also change with changing versions of PHP, leaving you with a very big headache if it happens. You could use ksort() to make sure the order is always the same, but that will slow things down a lot.

I've also read in various places, and in the comments in the manual, that get_object_vars() doesn't return static variables. That makes sense since all objects of a class share the same values for these variables, but it is something to keep in mind.

The storing and checking of the identity hashes, in some collection of hashes, will probably be the slowest part of this whole idea.

Then my final problem wilt this code: 

Properly written code would know the identity of its objects, or at least have a 100% reliable method to check this. Your code should be written in such a way that it already minimizes object duplication. This code seems the result of not being able to write good and efficient code (sorry, I'm trying to make a point here). 

For instance, many objects could already have a simply ID integer that identifies them. For instance a model class, based on a database row, would most likely have such an ID. Most other classes could, if needed, have a similar way to identify themselves. Once you combine such an ID with the class name you should have a 100% reliable identifier.

If you really need a way to identify various objects you could simply add an identity() method to them. Something like this:

&lt;?php

class MyClass 
{
    public function __construct($id)
    {
        $this-&gt;id = $id;
    }   

    public function identity() {
        return get_class() . ":" . $this-&gt;id;
    }
}

$myObject = new MyClass(999);

echo $myObject-&gt;identity();

?&gt;


This would return:


  MyClass:999


I agree that this is a very basic example, but it should be possible to do something similar for any class.

By writing such a specific identifier method for each class you can optimize it, which means it will be faster, and you can make it a 100% reliable under any circumstances. It is also a lot easier to debug, because you can see and read what is going on. No hiding behind mysterious hashes here.

Conclusion: Despite my objections I think your code looks fine. I do however wonder whether this approach will, in the end, cause more trouble than it is worth.

Note: There is more discussion in the comments. In the end coolpasta wrote a response to this question.