PHP.mk документација

SplObjectStorage::getHash

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

splobjectstorage.gethash.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека splobjectstorage.gethash.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
SplObjectStorage::getHash

Референца за `splobjectstorage.gethash.php` со подобрена типографија и навигација.

splobjectstorage.gethash.php

SplObjectStorage::getHash

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

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

= NULL

public SplObjectStorage::getHash(object $object): string

This method calculates an identifier for the objects added to an SplObjectStorage object.

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

The storage 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.

Вратени вредности

А string with the calculated identifier.

Errors/Exceptions

А RuntimeException is thrown when the returned value is not a string.

Примери

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

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

$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

Види Исто така

Белешки од корисници 1 белешка

aron dot duby at gmail dot com
пред 13 години
This also appears to be the function which gets used within the contains() function, so if all the objects you are storing already have a unique id you can overwrite this function with your own class.

<?php
class UserStorage extends SPLObjectStorage{
     public function getHash($obj){
          return $obj->id;
     }
}

$us = new UserStorage();
$user1 = new User(1);
$user2 = new User(2);

$us->attach($user1);
$us->attach($user2);

$me = new User(2);

// the following would normally fail since they are two separate objects 
// but it works now with our extended getHash()
$us->contains($me);

?>
Навигација

Прелистувај сродни теми и функции.

На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.