php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51345 Ability to override the hash function used by SplObjectStorage
Submitted: 2010-03-21 19:55 UTC Modified: 2010-04-29 17:49 UTC
From: tejas dot net at gmail dot com Assigned: colder (profile)
Status: Closed Package: SPL related
PHP Version: 5.3.2 OS: CentOS v5
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tejas dot net at gmail dot com
New email:
PHP Version: OS:

 

 [2010-03-21 19:55 UTC] tejas dot net at gmail dot com
Description:
------------
SplObjectStorage will be a lot more useful if I could override the hash function 
it uses, so different objects which are really equal will be recognized as such by 
SplObjectStorage.

While I currently do this by jumping through a few hoops, this will be a welcome 
addition. A __hashCode() magic method can accomplish this easily.

Test script:
---------------
Pseudo-code:
<?
class StockSecurity {
	public $exchange;
	public $ticker;
	
	public function __hashCode() {
		return $this->exchange . ":" . $this->ticker;
	}
}

$d = new SplObjectStorage();
$s = new StockSecurity( 'NASDAQ', 'GOOG' );
$d->attach( $s, getStockQuote( $s ) );

// later on, in a different method
$s = new StockSecurity( 'NASDAQ', 'GOOG' );
$quote = $d[ $s ];


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-29 17:49 UTC] colder@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: colder
 [2010-04-29 17:49 UTC] colder@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

SplObjectStorage now implements a getHash($obj) method, so you can extend 
SplObjectStorage and overwrite that method to suit your needs.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 05:01:28 2024 UTC