PHP - Ds\Hashable::equals() 函式



PHP 的 Ds\Hashable::equals() 函式用於確定一個物件是否等於當前例項。它允許物件用作結構(如 Ds\Map 和 Ds\Set)或任何其他遵循此介面的查詢結構中的鍵。

語法

以下是 PHP Ds\Hashable::equals() 函式的語法:

bool public Ds\Hashable::equals(mixed $obj)

引數

equals() 函式接受 $obj 引數,該引數是要與當前物件比較的物件。

返回值

如果物件被認為相等,則此函式返回 true;否則返回 FALSE。

PHP 版本

equals() 函式從 PECL Ds 擴充套件的 1.0.0 版本開始可用。

示例 1

此示例演示了透過比較兩個簡單物件來使用 PHP Ds\Hashable::equals() 函式的基本用法。

<?php
   // Create a Person class here
   class Person implements Ds\Hashable {
      private $name;
  
      public function __construct($name) {
          $this->name = $name;
      }
  
      public function equals($obj): bool {
          return $obj instanceof self && $this->name === $obj->name;
      }
  
      public function hash() {
          return $this->name;
      }
  }
  
  $person1 = new Person("Ankit");
  $person2 = new Person("Ankit");
  $person3 = new Person("Naveen");
  
  var_dump($person1->equals($person2)); 
  var_dump($person1->equals($person3)); 
?>

輸出

執行以上程式後,會生成以下輸出:

bool(true)
bool(false)

示例 2

在此示例中,我們將使用具有更多屬性的專案,然後我們將僅使用 equals() 函式比較名稱。

<?php
   // Create a User class here
   class User implements Ds\Hashable {
      private $name;
      private $age;
  
      public function __construct($name, $age) {
          $this->name = $name;
          $this->age = $age;
      }
  
      public function equals($obj): bool {
          return $obj instanceof self && $this->name === $obj->name && $this->age === $obj->age;
      }
  
      public function hash() {
          return $this->name . $this->age;
      }
  }
  
  $user1 = new User("Aman", 25);
  $user2 = new User("Aman", 25);
  $user3 = new User("Ankit", 30);
  
  var_dump($user1->equals($user2)); 
  var_dump($user1->equals($user3)); 
?> 

輸出

以上程式碼將產生類似以下的結果:

bool(true)
bool(false)

示例 3

此示例演示了我們將使用 equals() 函式比較專案,這些專案的特徵由其他物件共享。現在,以下程式碼從 equals() 中檢索特定資訊,並將其打印出來。

<?php
   // Create Address class here
   class Address implements Ds\Hashable {
      private $city;
  
      public function __construct($city) {
          $this->city = $city;
      }
  
      public function equals($obj): bool {
          return $obj instanceof self && $this->city === $obj->city;
      }
  
      public function hash() {
          return $this->city;
      }
  }

  // Create Employee class here
  class Employee implements Ds\Hashable {
      private $name;
      private $address;
  
      public function __construct($name, Address $address) {
          $this->name = $name;
          $this->address = $address;
      }
  
      public function equals($obj): bool {
          return $obj instanceof self && $this->name === $obj->name && $this->address->equals($obj->address);
      }
  
      public function hash() {
          return $this->name . $this->address->hash();
      }
  }
  
  $address1 = new Address("New Delhi");
  $address2 = new Address("New Delhi");
  $employee1 = new Employee("Ankit", $address1);
  $employee2 = new Employee("Ankit", $address2);
  $employee3 = new Employee("Ankit", new Address("Mumbai"));
  
  var_dump($employee1->equals($employee2)); 
  var_dump($employee1->equals($employee3)); 
?> 

輸出

這將建立以下輸出:

bool(true)
bool(false)
php_function_reference.htm
廣告