grifart / equalable-utils
Provides a universal equals() function.
Installs: 12 016
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 1
Requires
- php: >= 7.1.0
- grifart/comparable-polyfill: ^1.0.0
- php-ds/php-ds: ^1.1
Requires (Dev)
- nette/tester: ^2.0.0
This package is auto-updated.
Last update: 2024-12-17 07:44:11 UTC
README
Are objects equal or not? PHP defines only instance equality using ===
operator. Then there is comparision by value ==
which compares by object values, but user-land code cannot modify how they are compared.
This is why universal equals()
method exists in Java. PHP is missing anything like that.
This is why I have implemented global equals()
function provides logic equality of objects. Which objects are equal and which not can be modified by several things - see bellow.
Installation
composer require grifart/equalable-utils
Implementing logical equality
There are several ways of implementhing logic equality.
\Ds\Hashable
(from PHP DS extension)
Hashable is PHP clone of Java equals()
and hashCode()
method. If you like to use implementing objects in collection this would make the most sense.
\Comparable
interface proposal
This library supports \Comparable
interface and tries to compare objects.
Just implement equals($other), is($other) or isEqualTo($other) methods
This methods must behave the same as Java equals()
method.