Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the fastest way to compare two objects in PHP?

Tags:

object

php

Let's say I have an object - a User object in this case - and I'd like to be able to track changes to with a separate class. The User object should not have to change it's behavior in any way for this to happen.

Therefore, my separate class creates a "clean" copy of it, stores it somewhere locally, and then later can compare the User object to the original version to see if anything changed during its lifespan.

Is there a function, a pattern, or anything that can quickly compare the two versions of the User object?

Option 1 Maybe I could serialize each version, and directly compare, or hash them and compare?

Option 2 Maybe I should simply create a ReflectionClass, run through each of the properties of the class and see if the two versions have the same property values?

Option 3 Maybe there is a simple native function like objects_are_equal($object1,$object2);?

What's the fastest way to do this?

like image 393
johnnietheblack Avatar asked Mar 26 '12 05:03

johnnietheblack


People also ask

How to compare objects in PHP?

To compare objects in PHP, you can use either the comparison operator (==) or identity operator (===). However, each operator behaves differently based on two main criteria: Objects are the same instance or different instances of a class

How do you compare two objects in Python?

Comparing Objects¶ When using the comparison operator (==), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values (values are compared with ==), and are instances of the same class.

How to compare two objects with the same properties?

When you compare objects using the comparison operator ( == ), two objects are equal if they are instances of the same class and have the same properties and values. First, create two new Point objects with the same properties’ values and compare them:

Are PHP's comparison operators transitive?

Note: according to the spec, PHP's comparison operators are not transitive. For example, the following are all true in PHP5: As a result, the outcome of sorting an array depends on the order the elements appear in the pre-sort array. The following code will dump out two arrays with *different* orderings:


2 Answers

Simple example comparing dto class through reflection

class InvoiceDetails
{
    /** @var string */
    public $type;

    /** @var string */
    public $contractor;

    /** @var string */
    public $invoiceNumber;

    /** @var \DateTimeInterface|null */
    public $saleDate;

    /** @var \DateTimeInterface|null */
    public $issueDate;

    /** @var \DateTimeInterface|null */
    public $dueDate;

    /** @var string */
    public $payment;

    /** @var string */
    public $status;


    public function isEqual(InvoiceDetails $details): bool
    {
        $reflection = new \ReflectionClass(self::class);

        /** @var \ReflectionProperty $property */
        foreach ($reflection->getProperties() as $property) {
            $name = $property->getName();
            if ($this->$name !== $details->$name) {
                return false;
            }
        }

        return true;
    }

}
like image 156
Jan Galtowski Avatar answered Oct 11 '22 23:10

Jan Galtowski


The only way to access the all (public, protected and private) properties of an object without modifying it is through reflection. You could, if you wanted to, clone the object before it's modified and then compare them by using the reflection functions to loop through the different properties of one of the two objects and comparing values. That would effectively tell you what properties have changed.

If all you care to see is whether or not the objects have changed, you can use the == or === operators. Have a look at the comparing objects section of PHP's documentation.

That said, wouldn't just be easier to keep track of what you've changed as you change them?

like image 27
Francois Deschenes Avatar answered Oct 12 '22 00:10

Francois Deschenes