PHP - Ds\Hashable::equals() Function
The PHP Ds\Hashable::equals() function is used to determine whether an object is equal to the current instance. It allows objects to be used as keys in structures such as Ds\Map and Ds\Set, or any other lookup structure that honors this interface.
Syntax
Below is the syntax of the PHP Ds\Hashable::equals() function −
bool public Ds\Hashable::equals(mixed $obj)
Parameters
The equals() function accepts $obj parameter which is an object to compare with the current object.
Return Value
This function returns true if the objects are considered equal or FALSE on failure.
PHP Version
The equals() function is available from version 1.0.0 of the PECL Ds extension onwards.
Example 1
This example shows the basic usage of the the PHP Ds\Hashable::equals() function by comparing two simple objects.
<?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));
?>
Output
After running the above program, it generates the following output −
bool(true) bool(false)
Example 2
In this example, we will use items with more attributes and than we will compare only names with the help of equals() function.
<?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));
?>
Output
The above code will result something like this −
bool(true) bool(false)
Example 3
This example demonstrates we will compare items using the equals() function, whose characteristics are shared by other objects. Now, the below code retrieves specific information from equals(), and prints it.
<?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));
?>
Output
This will create the below output −
bool(true) bool(false)