2017 © Pedro Peláez
 

library equality

A better strict comparison for PHP.

image

eloquent/equality

A better strict comparison for PHP.

  • Tuesday, February 11, 2014
  • by ezzatron
  • Repository
  • 2 Watchers
  • 12 Stars
  • 6,916 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 1 % Grown

The README.md

Equality

A better strict comparison for PHP., (*1)

The most recent stable version is 2.1.2 Current build status image Current coverage status image, (*2)

Deprecated

Equality is deprecated. Please use Parity instead., (*3)

Installation and documentation

The problem

Sometimes it is necessary to compare two objects to determine whether they are considered equal., (*4)

If the == operator is used, there is no strictness about the equality. For instance, this snippet outputs 'equal':, (*5)

$left = new stdClass;
$left->foo = 0;

$right = new stdClass;
$right->foo = null;

if ($left == $right) {
    echo 'equal';
} else {
    echo 'not equal';
}

Conversely, if the === operator is used, objects are not equal unless they are the same instance. The following snippet outputs 'not equal':, (*6)

$left = new stdClass;
$left->foo = 'bar';

$right = new stdClass;
$right->foo = 'bar';

if ($left === $right) {
    echo 'equal';
} else {
    echo 'not equal';
}

Unfortunately PHP does not have an inbuilt method to compare objects strictly without requiring that they be the same instance. This is where Equality comes in. This snippet correctly outputs 'equal':, (*7)

use Eloquent\Equality\Comparator;

$left = new stdClass;
$left->foo = 'bar';

$right = new stdClass;
$right->foo = 'bar';

$comparator = new Comparator;

if ($comparator->equals($left, $right)) {
    echo 'equal';
} else {
    echo 'not equal';
}

Usage

Equality is very simple to use. Simply instantiate a Comparator and use its equals() method:, (*8)

use Eloquent\Equality\Comparator;

$comparator = new Comparator;

if ($comparator->equals($left, $right)) {
    // equal
} else {
    // not equal
}

Equality can work with any PHP data type, not just objects., (*9)

Custom equality logic

In some cases it may be desirable to customize how equality is determined. The interface EqualityComparable can be used to provide a custom equality implementation for any class:, (*10)

use Eloquent\Equality\Comparator;
use Eloquent\Equality\EqualityComparable;

class Foo implements EqualityComparable
{
    /**
     * @param mixed $value
     * @param Comparator $comparator
     *
     * @return boolean
     */
    public function isEqualTo($value, Comparator $comparator)
    {
        // custom logic...
    }
}

When Equality encounters an object that implements EqualityComparable, it will return the result of the isEqualTo() method instead of using the default equality logic. The comparator itself will be passed as the second parameter., (*11)

How does Equality work?

Equality uses reflection to recurse over the values it is passed and ensure that they are deeply, and strictly, equal., (*12)

In addition, it implements special protections to avoid infinite recursion issues, such as objects that contain themselves, or objects that contain the object that they are being compared to., (*13)

The Versions

11/02 2014

dev-develop

dev-develop https://github.com/eloquent/equality

A better strict comparison for PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

object comparison strict equality deep

11/02 2014

dev-master

9999999-dev https://github.com/eloquent/equality

A better strict comparison for PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

object comparison strict equality deep

11/02 2014

2.1.2

2.1.2.0 https://github.com/eloquent/equality

A better strict comparison for PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

object comparison strict equality deep

04/03 2013

2.1.1

2.1.1.0 https://github.com/eloquent/equality

A better strict comparison for PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

object comparison strict equality deep

18/01 2013

2.1.0

2.1.0.0 https://github.com/eloquent/equality

A better strict comparison for PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

  • icecave/testing ~1

object comparison strict equality deep

15/11 2012

2.0.1

2.0.1.0 https://github.com/eloquent/equality

A better strict comparison for PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

object comparison strict equality deep

09/11 2012

2.0.0

2.0.0.0 https://github.com/eloquent/equality

A better strict comparison for PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

object comparison strict equality deep

09/11 2012

1.2.0

1.2.0.0 https://github.com/eloquent/equality

A better strict comparison for PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

object comparison strict equality deep

09/11 2012

1.1.1

1.1.1.0 https://github.com/eloquent/equality

A better strict comparison for PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

object comparison strict equality deep

08/11 2012

1.1.0

1.1.0.0 https://github.com/eloquent/equality

A better strict comparison for PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

object comparison strict equality deep

08/11 2012

1.0.0

1.0.0.0 https://github.com/eloquent/equality

A better strict comparison for PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

object comparison strict equality deep