<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Calculate a unique identifier for the contained objects</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="splobjectstorage.detach.html">SplObjectStorage::detach</a></div> <div class="next" style="text-align: right; float: right;"><a href="splobjectstorage.getinfo.html">SplObjectStorage::getInfo</a></div> <div class="up"><a href="class.splobjectstorage.html">SplObjectStorage</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="splobjectstorage.gethash" class="refentry"> <div class="refnamediv"> <h1 class="refname">SplObjectStorage::getHash</h1> <p class="verinfo">(PHP 5 >= 5.4.0)</p><p class="refpurpose"><span class="refname">SplObjectStorage::getHash</span> — <span class="dc-title"> Calculate a unique identifier for the contained objects </span></p> </div> <div class="refsect1 description" id="refsect1-splobjectstorage.gethash-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">string</span> <span class="methodname"><strong>SplObjectStorage::getHash</strong></span> ( <span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span> )</div> <p class="para rdfs-comment"> This method calculates an identifier for the objects added to an <a href="class.splobjectstorage.html" class="classname">SplObjectStorage</a> object. </p> <p class="para"> The implementation in <a href="class.splobjectstorage.html" class="classname">SplObjectStorage</a> returns the same value as <span class="function"><a href="function.spl-object-hash.html" class="function">spl_object_hash()</a></span>. </p> <p class="para"> 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. </p> </div> <div class="refsect1 parameters" id="refsect1-splobjectstorage.gethash-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <span class="term"><em><code class="parameter">object</code></em></span> <dd> <p class="para"> The object whose identifier is to be calculated. </p> </dd> </dt> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-splobjectstorage.gethash-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> A <span class="type"><a href="language.types.string.html" class="type string">string</a></span> with the calculated identifier. </p> </div> <div class="refsect1 errors" id="refsect1-splobjectstorage.gethash-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> A <a href="class.runtimeexception.html" class="classname">RuntimeException</a> is thrown when the returned value is not a <span class="type"><a href="language.types.string.html" class="type string">string</a></span>. </p> </div> <div class="refsect1 examples" id="refsect1-splobjectstorage.gethash-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-3815"> <p><strong>Example #1 <span class="function"><strong>SplObjectStorage::getHash()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">OneSpecimenPerClassStorage </span><span style="color: #007700">extends </span><span style="color: #0000BB">SplObjectStorage </span><span style="color: #007700">{<br /> public function </span><span style="color: #0000BB">getHash</span><span style="color: #007700">(</span><span style="color: #0000BB">$o</span><span style="color: #007700">) {<br /> return </span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$o</span><span style="color: #007700">);<br /> }<br />}<br />class </span><span style="color: #0000BB">A </span><span style="color: #007700">{}<br /><br /></span><span style="color: #0000BB">$s </span><span style="color: #007700">= new </span><span style="color: #0000BB">OneSpecimenPerClassStorage</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$o1 </span><span style="color: #007700">= new </span><span style="color: #0000BB">stdClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$o2 </span><span style="color: #007700">= new </span><span style="color: #0000BB">stdClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$o3 </span><span style="color: #007700">= new </span><span style="color: #0000BB">A</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$s</span><span style="color: #007700">[</span><span style="color: #0000BB">$o1</span><span style="color: #007700">] = </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//$o2 is considered equal to $o1 so the value is replaced<br /></span><span style="color: #0000BB">$s</span><span style="color: #007700">[</span><span style="color: #0000BB">$o2</span><span style="color: #007700">] = </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$s</span><span style="color: #007700">[</span><span style="color: #0000BB">$o3</span><span style="color: #007700">] = </span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//these are considered equal to the objects before<br />//so they can be used to access the values stored under them<br /></span><span style="color: #0000BB">$p1 </span><span style="color: #007700">= new </span><span style="color: #0000BB">stdClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$p2 </span><span style="color: #007700">= new </span><span style="color: #0000BB">A</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$s</span><span style="color: #007700">[</span><span style="color: #0000BB">$p1</span><span style="color: #007700">], </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$s</span><span style="color: #007700">[</span><span style="color: #0000BB">$p2</span><span style="color: #007700">], </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 2 3 </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-splobjectstorage.gethash-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.spl-object-hash.html" class="function" rel="rdfs-seeAlso">spl_object_hash()</a> - Return hash id for given object</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="splobjectstorage.detach.html">SplObjectStorage::detach</a></div> <div class="next" style="text-align: right; float: right;"><a href="splobjectstorage.getinfo.html">SplObjectStorage::getInfo</a></div> <div class="up"><a href="class.splobjectstorage.html">SplObjectStorage</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>