Object Comparison

Pitting Code Titans Against Each Other

PHP allows you to compare objects with the comparison and the identity operators. When using the comparison operator, PHP checks to see if two objects have the same attributes and values, and if they’re instances of the same class. With the identity operator, you check to see if the object variables point to the same instance of the same class.

Recap: Review the code from the previous article.

https://github.com/dinocajic/php-youtube-tutorials/tree/master

https://www.dinocajic.com/php-final-keyword/

Aside from the Lamborghini class, we’re going to recreate the Ferrari class that we once had.

<?php
require_once("Car.php");

class Lamborghini extends Car {

}
<?php
require_once("Car.php");

class Ferrari extends Car {

}

Both of these classes extend the Car class. The only difference is their name. Now it’s time for some testing. Brief and to the point.

<?php
// Object Comparison
require_once("Car.php");
require_once("Lamborghini.php");
require_once("Ferrari.php");

$lamborghini_1 = new Lamborghini(1999, "Lamborghini", "Diablo");
$lamborghini_2 = new Lamborghini(1999, "Lamborghini", "Diablo");

$lamborghini_3 = $lamborghini_2;

var_dump( $lamborghini_1 == $lamborghini_1 );
var_dump( $lamborghini_1 === $lamborghini_1 );
var_dump( $lamborghini_1 == $lamborghini_2 );
var_dump( $lamborghini_1 === $lamborghini_2 );
var_dump( $lamborghini_2 === $lamborghini_3 );

We’ll create two Lamborghini objects, $lamborghini_1 and $lamborghini_2. They’ll be instantiated with the exact same constructor arguments. We’ll then create $lamborghini_3 and set it equal to $lamborghini_2. Let’s see the results that we get when comparing these 3 objects. We’ll use the comparison operator (==) and the identity operator (===) for these tests.

  • var_dump( $lamborghini_1 == $lamborghini_1 ) yields true. This is expected since we’re looking at the same object. But, let’s look at the identity operator next and see if we get the same result.
  • var_dump( $lamborghini_1 === $lamborghini_1 ) yields true. This is another expected result. These 2 objects are in fact the same object, so of course it would be true.
  • var_dump( $lamborghini_1 == $lamborghini_2 ) yields true. After all, both objects are instantiated using the same constructor arguments so reason would dictate that they are equal. But are they identical? Let’s look at that next.
  • var_dump( $lamborghini_1 === $lamborghini_2 ) yields false. These two objects are not identical since they’re not the same object. The variables are pointing to different areas within memory.
  • var_dump( $lamborghini_2 === $lamborghini_3 ) yields true. This is interesting. They have different variable names but they’re actually pointing to the same area in memory, making them identical. When you assign a variable to another object, it’s not making a copy of the object, it just assigns a reference to that object’s memory location. This means that if you modified a property within the $lamborghini_3 object, you would also be modifying $lamborghini_2 since they point to the same object in memory. Let’s see that with an example.
<?php
// Object Comparison
require_once("Lamborghini.php");
require_once("Ferrari.php");

$lamborghini_1 = new Lamborghini(1999, "Lamborghini", "Diablo");
$lamborghini_2 = new Lamborghini(1999, "Lamborghini", "Diablo");

$lamborghini_3 = $lamborghini_2;

var_dump( $lamborghini_1 == $lamborghini_1 );
var_dump( $lamborghini_1 === $lamborghini_1 );
var_dump( $lamborghini_1 == $lamborghini_2 );
var_dump( $lamborghini_1 === $lamborghini_2 );
var_dump( $lamborghini_2 === $lamborghini_3 );

$lamborghini_3->setYear(1998);
var_dump( $lamborghini_2->getYear() );

We’re modifying the year of the $lamborghini_3 object and we’re using var_dump to display the year of the $lamborghini_2 object. Should the result be 1999 as was instantiated or 1998 for $lamborghini_2? The answer is 1998. Remember, both $lamborghini_2 and $lamborghini_3 point to the same object in memory.

Let’s look at another example with different classes. We’ll create a Ferrari object with the same constructor arguments as our Lamborghini object and see if we can trick PHP into thinking it’s the same class.

 

And the result we get is…false. PHP is smart enough to know that these are two difference objects even though they both extend the Car class. The class names are different so the objects are different in PHP’s eyes.

Final Keyword

STAMPING ENDURING MARKS ON CODE’S PATH WITH ‘FINAL’

PHP – P65:FINAL KEYWORD

he final keyword can be prefixed to methods and classes. If you prefix a method in the parent class, and the child class inherits the methods from the parent, the final keyword prevents the child class from overriding that method.

Object Comparison

Pitting Code Titans Against Each Other

PHP – P66:object comparison

When using the comparison operator, PHP checks to see if two objects have the same attributes and values, and if they’re instances of the same class.

Parent Constructor

PASSING DOWN CONSTRUCTOR LEGACIES FROM ANCESTORS

PHP – P67:PARENT CONSTRUCTOR

If the child class does not define a constructor, it inherits the parent constructor like any other method. If a child class defines a constructor of its own, the parent constructor will be overridden.

Leave a Reply