<!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>Returns a scalar value to be used as a hash value</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ds-hashable.equals.html">Ds\Hashable::equals</a></div> <div class="next" style="text-align: right; float: right;"><a href="class.ds-sequence.html">Sequence</a></div> <div class="up"><a href="class.ds-hashable.html">Hashable</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="ds-hashable.hash" class="refentry"> <div class="refnamediv"> <h1 class="refname">Ds\Hashable::hash</h1> <p class="verinfo">(PECL ds >= 1.0.0)</p><p class="refpurpose"><span class="refname">Ds\Hashable::hash</span> — <span class="dc-title">Returns a scalar value to be used as a hash value</span></p> </div> <div class="refsect1 description" id="refsect1-ds-hashable.hash-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">abstract</span> <span class="modifier">public</span> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>Ds\Hashable::hash</strong></span> ( <span class="methodparam">void</span> )</div> <p class="para rdfs-comment"> Returns a scalar value to be used as the hash value of the objects. </p> <p class="para"> While the hash value does not define equality, all objects that are equal according to <span class="function"><a href="ds-hashable.equals.html" class="function">Ds\Hashable::equals()</a></span> must have the same hash value. Hash values of equal objects don't have to be unique, for example you could just return <strong><code>TRUE</code></strong> 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. </p> <p class="para"> This method allows objects to be used as keys in structures such as <strong class="classname">Ds\Map</strong> and <strong class="classname">Ds\Set</strong>, or any other lookup structure that honors this interface. </p> <div class="caution"><strong class="caution">Caution</strong> <p class="para"> 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. </p> </div> <div class="caution"><strong class="caution">Caution</strong> <p class="para"> All objects that are equal must have the same hash value. </p> </div> </div> <div class="refsect1 parameters" id="refsect1-ds-hashable.hash-parameters"> <h3 class="title">Parameters</h3> <p class="para">This function has no parameters.</p> </div> <div class="refsect1 returnvalues" id="refsect1-ds-hashable.hash-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> A scalar value to be used as this object's hash value. </p> </div> <div class="refsect1 examples" id="refsect1-ds-hashable.hash-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-5133"> <p><strong>Example #1 <span class="function"><strong>Ds\Hashable::hash()</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">HashableObject </span><span style="color: #007700">implements \</span><span style="color: #0000BB">Ds</span><span style="color: #007700">\</span><span style="color: #0000BB">Hashable<br /></span><span style="color: #007700">{<br /> private </span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br /> private </span><span style="color: #0000BB">$email</span><span style="color: #007700">;<br /><br /> public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">, </span><span style="color: #0000BB">$email</span><span style="color: #007700">)<br /> {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">email </span><span style="color: #007700">= </span><span style="color: #0000BB">$email</span><span style="color: #007700">;<br /> }<br /><br /> </span><span style="color: #FF8000">/**<br /> * Should return the same value for all equal objects, but doesn't have to<br /> * be unique. This value will not be used to determine equality.<br /> */<br /> </span><span style="color: #007700">public function </span><span style="color: #0000BB">hash</span><span style="color: #007700">()<br /> {<br /> return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">email</span><span style="color: #007700">;<br /> }<br /><br /> </span><span style="color: #FF8000">/**<br /> * This determines equality, usually during a hash table lookup to determine<br /> * if the bucket's key matches the lookup key. The hash has to be equal if<br /> * the objects are equal, otherwise this determination wouldn't be reached.<br /> */<br /> </span><span style="color: #007700">public function </span><span style="color: #0000BB">equals</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">): </span><span style="color: #0000BB">bool<br /> </span><span style="color: #007700">{<br /> return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">=== </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-></span><span style="color: #0000BB">name<br /> </span><span style="color: #007700">&& </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">email </span><span style="color: #007700">=== </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-></span><span style="color: #0000BB">email</span><span style="color: #007700">;<br /> }<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ds-hashable.equals.html">Ds\Hashable::equals</a></div> <div class="next" style="text-align: right; float: right;"><a href="class.ds-sequence.html">Sequence</a></div> <div class="up"><a href="class.ds-hashable.html">Hashable</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>