PHP - Ds\Hashable::hash() 函数



PHP 的 Ds\Hashable::hash() 函数用于确定对象的哈希值。当使用对象作为基于哈希的集合(如哈希表或集合)的键时,此函数非常有用。

语法

以下是PHP Ds\Hashable::hash() 函数的语法:

mixed public Ds\Hashable::hash()

参数

hash() 函数不接受任何参数。

返回值

此函数返回一个标量值,用作哈希值。

PHP版本

hash() 函数从 PECL Ds 扩展的 1.0.0 版本开始可用。

示例1

首先,我们将向您展示PHP Ds\Hashable::hash() 函数的基本示例,以获取对象的哈希值。

<?php
   // Create a class Person and implement Ds\Hashable 
   class Person implements Ds\Hashable {
      private $name;
      private $age;
  
      public function __construct($name, $age) {
          $this->name = $name;
          $this->age = $age;
      }
  
      public function hash() {
          return md5($this->name . $this->age);
      }
  
      public function equals($obj): bool {
          return $this->name === $obj->name && $this->age === $obj->age;
      }
  }
  
  $person = new Person("Ankit", 33);
  echo $person->hash();
  
?>

输出

以上代码将产生类似这样的结果:

4ad162f31dbd28172e2dc04a2f033f47

示例2

此示例演示如何在一个集合中使用可哈希对象,以确保根据哈希值的唯一性。

<?php
   // Create a Product class and implement Ds\Hashable
   class Product implements Ds\Hashable {
      private $id;
      private $name;
  
      public function __construct($id, $name) {
          $this->id = $id;
          $this->name = $name;
      }
  
      public function hash() {
          return $this->id;
      }
  
      public function equals($obj): bool {
          return $this->id === $obj->id;
      }
  }
  
  $product1 = new Product(101, "Laptop");
  $product2 = new Product(102, "Smartphone");
  
  $set = new Ds\Set();
  $set->add($product1);
  $set->add($product2);
  
  foreach ($set as $product) {
      echo $product->hash() . "\n";
  }
?> 

输出

运行上述程序后,它将生成以下输出:

101
102

示例3

现在,我们将使用hash()函数为组合多个属性的对象创建一个自定义哈希函数。

<?php
   // Create a class Book and implement Ds\Hashable
   class Book implements Ds\Hashable {
      private $title;
      private $author;
  
      public function __construct($title, $author) {
          $this->title = $title;
          $this->author = $author;
      }
  
      public function hash() {
          return sha1($this->title . $this->author);
      }
  
      public function equals($obj): bool {
          return $this->title === $obj->title && $this->author === $obj->author;
      }
  }
  
  $book = new Book("1984", "George Orwell");
  echo "Hash value is as follows: \n";
  echo $book->hash();
?> 

输出

这将创建以下输出:

Hash value is as follows:
f54227c9321449aea0c1ddcd9d02228a09b6bcbc

示例4

在下面的示例中,我们使用hash()函数来比较两个对象,并查看它们根据其哈希值是否相等。

<?php
   // Create a Car class and implement Ds\Hashable
   class Car implements Ds\Hashable {
      private $make;
      private $model;
  
      public function __construct($make, $model) {
          $this->make = $make;
          $this->model = $model;
      }
  
      public function hash() {
          return crc32($this->make . $this->model);
      }
  
      public function equals($obj): bool {
          return $this->make === $obj->make && $this->model === $obj->model;
      }
  }
  
  $car1 = new Car("Maruti Suzuki", "Baleno");
  $car2 = new Car("Maruti Suzuki", "Baleno");
  
  if ($car1->equals($car2)) {
      echo "Cars are identical.";
  } else {
      echo "Cars are different.";
  }
?> 

输出

运行上述程序后,它将产生以下输出:

Cars are identical.
php_function_reference.htm
广告