Hello, internals!

I like the idea to assign a custom identifier to the object, but why this
should be only in the core? Java has a method 'hashCode' which can be used
to return an unique identifier for specific object. So, my suggestion is to
add 'Hashable' interface for that into the PHP.

Then, each developer can use a default implementation which will return
spl_object_id/spl_object_hash/whatever or implement this interface to
control the unique identifier of current object:

class Test implements Hashable
{
    private static $objectCounter = 100500;
    private $identifier = null;
    public function __construct()
    {
        $this->identifier = self::$objectCounter++;
    }

    public function hashCode()
    {
        return __CLASS__ . ':'. $this->identifier;
    }
}

echo spl_object_hash(new Test); // Will return 'Test:100500'

This solution is more flexible and can give a freedom for implementations.
What do you think about this random thought?

Thanks.

2015-08-03 10:20 GMT+03:00 Nicolas Grekas <nicolas.grekas+...@gmail.com>:

> 2015-08-02 20:03 GMT+02:00 Rowan Collins <rowan.coll...@gmail.com>:
>
> > On 02/08/2015 18:41, Bob Weinand wrote:
> >
> >> Some suspicious use of spl_object_hash() out there...
> >>> >
> >>>
> >>>> >>
> >>>>
> https://github.com/symfony/symfony/blob/master/src/Symfony/Component/VarDumper/Cloner/VarCloner.php
> >>>>
> >>> >
> >>> >Not sure what this one does... but calculations with spl_object_hash()
> >>> >look very suspicious.
> >>>
> >> Actually, it's doing the right thing… calculating the value the object
> id
> >> is xor'ed with (as we know that consecutively defined objects have
> >> consecutive ids).
> >> It's relying on the implementation of spl_object_hash() and will even
> >> continue to work when we remove that part of randomness as that value
> it's
> >> xor'ed with is then nothing else than 0.
> >>
> >
> > The right thing for what purpose? Why does it need that ID, rather than
> > the value that spl_object_hash() gave in the first place? Just to be
> > prettier to the user?
> >
>
>
> For the purpose of providing an id that humans can read and compare, to
> easily spot if two objects are identical or not. Try comparing two
> spl_object_hashes and you'll understand the need...
>
> The id as displayed by e.g. var_dump is not an implementation detail that
> leaks through it.
> It's really an important feature of the output.
>
> I learned that while implementing VarDumper (see link above), by user
> requests.
>
> Regards,
> Nicolas
>

Reply via email to