SplObjectStorage::getHash

(Информация о версии неизвестна, возможно, только в SVN)

SplObjectStorage::getHash Calculate a unique identifier for the contained objects

Описание

public string SplObjectStorage::getHash ( string $object )

The function calculates an identifier for the objects added to this SplObjectStorage object.

The implementation in SplObjectStorage returns the same value as spl_object_hash().

This identifier must be unique among the objects contained. This means this object will never contain more than one object with the same identifier. As such, it can be used to implement a set (a collection of unique values) where the quality of an object being unique is determined by the value returned by this function being unique.

Список параметров

object

The object whose identifier is to be calculated.

Возвращаемые значения

A string with the calculated identifier. An exception is thrown if any other type is returned.

Примеры

Пример #1 SplObjectStorage::getHash() example

<?php
class OneSpecimenPerClassStorage extends SplObjectStorage {
    public function 
getHash($o) {
        return 
get_class($o);
    }
}
class 
{}

$s = new OneSpecimenPerClassStorage;
$o1 = new stdClass;
$o2 = new stdClass;
$o3 = new A;

$s[$o1] = 1;
//$o2 is considered equal to $o1 so the value is replaced
$s[$o2] = 2;
$s[$o3] = 3;

//these are considered equal to the objects before
//so they can be used to access the values stored under them
$p1 = new stdClass;
$p2 = new A;
echo 
$s[$p1], "\n";
echo 
$s[$p2], "\n";
?>

Результатом выполнения данного примера будет что-то подобное:

2
3


Участник рейтинга Тэглайн 2010