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
 [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-2020 The PHP Group
All rights reserved.
Last updated: Sun Nov 29 04:01:23 2020 UTC