Ds\Hashable::hash

(PECL ds >= 1.0.0)

Ds\Hashable::hashReturns a scalar value to be used as a hash value

说明

abstract public Ds\Hashable::hash ( void ) : mixed

Returns a scalar value to be used as the hash value of the objects.

While the hash value does not define equality, all objects that are equal according to Ds\Hashable::equals() must have the same hash value. Hash values of equal objects don't have to be unique, for example you could just return TRUE for all objects and nothing would break - the only implication would be that hash tables then turn into linked lists because all your objects will be hashed to the same bucket. It's therefore very important that you pick a good hash value, such as an ID or email address.

This method allows objects to be used as keys in structures such as Ds\Map and Ds\Set, or any other lookup structure that honors this interface.

Caution

Do not pick a value that might change within the object, such as a public property. Hash table lookups would fail because the hash has changed.

Caution

All objects that are equal must have the same hash value.

参数

此函数没有参数。

返回值

A scalar value to be used as this object's hash value.

范例

Example #1 Ds\Hashable::hash() example

<?php
class HashableObject implements \Ds\Hashable
{
    private 
$name;
    private 
$email;

    public function 
__construct($name$email)
    {
        
$this->name  $name;
        
$this->email $email;
    }

    
/**
     * Should return the same value for all equal objects, but doesn't have to
     * be unique. This value will not be used to determine equality.
     */
    
public function hash()
    {
        return 
$this->email;
    }

    
/**
     * This determines equality, usually during a hash table lookup to determine
     * if the bucket's key matches the lookup key. The hash has to be equal if
     * the objects are equal, otherwise this determination wouldn't be reached.
     */
    
public function equals($obj): bool
    
{
        return 
$this->name  === $obj->name
            
&& $this->email === $obj->email;
    }
}
?>
相关文章
php deque returns the result of applying a callback to each valuephp hashable returns a scalar value to be used as a hash valuephp mcve 函数 returns array of strings which represents the keys that can be used for response parameters on this transactionphp 函数 returns the bson representation of a php valuephp runkit 函数 determines if the current functions return value will be usedphp svn 函数 creates and returns a stream that will be used to replacephp zlib 函数 returns the coding type used for output compressionphp imagickdraw returns the shape to be used at the end of open subpaths when they are strokedphp imagickdraw returns the shape to be used at the corners of paths when they are strokedphp imagickdraw specifies the shape to be used at the end of open subpaths when they are strokedphp mongodb bson undefined returns a representation that can be converted to jsonphp mongogridfsfile returns a resource that can be used to read the stored filephp recursiveregexiterator returns whether an iterator can be obtained for the current entryphp reflectionparameter returns whether this parameter can be passed by valuephp solrquery returns the minimum counts for facet fields should be included in the responsephp solrquery returns an offset into the list of constraints to be used for paginationphp solrquery returns the group.ngroups valuephp solrquery returns the minimum word length below which words will be ignoredphp solrquery returns the minimum document frequency to return in order to be includedphp tokyotyrant returns the size of the value
关注编程学问公众号