php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52528 request for interface hashCode (Identifiable)
Submitted: 2010-08-03 23:05 UTC Modified: 2015-03-14 15:40 UTC
Votes:3
Avg. Score:3.7 ± 1.9
Reproduced:2 of 3 (66.7%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: giorgio dot liscio at email dot it Assigned:
Status: Wont fix Package: Class/Object related
PHP Version: Irrelevant OS: irr
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
7 + 29 = ?
Subscribe to this entry?

 
 [2010-08-03 23:05 UTC] giorgio dot liscio at email dot it
Description:
------------
i, i hope to see something like this in future php releases


interface Identifiable
{
     public function /*string*/ hashCode();
}


class Test implements Identifiable
{
     public function hashCode(){return spl_object_hash($this);}
}


method hashCode() should be called in these contexts:


$myarr[new Test()] = "test";
$myarrobj->{new Test()} = "test";
and manually inside:
function method(Identifiable $instance){};


the hash should be obtained by calling

$obj->hashCode()     if it is Identifiable or
$obj->scalarValue()  if it implements ScalarAccess
$obj->__toString()   if it has method __toString

or, finally throw an error

what do you think about this?
thank you



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-03 23:20 UTC] jackohman at eugama dot com
would be really nice having the possibility to use different output strings, one for readability, one for unique identify an object

class ArrayObject implements Identifiable
{
      function hashCode(){return spl_object_hash($this);}
      function __toString(){return json_encode($this);}
}

$obj = new ArrayObject();
$myarr[$obj] = (String)$obj;

very nice, i'm voting it
 [2010-08-04 00:32 UTC] felipe@php.net
-Type: Bug +Type: Feature/Change Request
 [2010-10-03 18:51 UTC] + at ni-po dot com
I see no reason for this to be implemented. Generate an spl_object_hash, use (string) casting or implement your own ->toHash() method.

This is something you will want to do only ***very*** rarely. Thus it shouldn't pollute the interface namespace.
 [2015-03-14 15:40 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2015-03-14 15:40 UTC] nikic@php.net
RFC for this https://wiki.php.net/rfc/objkey has been declined, so closing this as Won't Fix.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 21:01:36 2024 UTC