Cara menggunakan comparing objects in php

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.

php-youtube-tutorials/65 Final Keyword

Code for the PHP YouTube tutorials. Contribute to dinocajic/php-youtube-tutorials development by creating an…

github.com

PHP — P65: Final Keyword

The final keyword can be prefixed to methods and classes. If you prefix a method in the parent class, and the child…

blog.devgenius.io

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

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.

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.

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.

dinocajic/php-youtube-tutorials

Code for the PHP YouTube tutorials.

github.com

Cara menggunakan comparing objects in php

In this course, you'll learn the fundamentals of PHP programming. You'll start with the basics, learning how PHP works and writing simple PHP loops and functions. Then you'll build up to coding classes for simple object-oriented programming (OOP). Along the way, you'll learn all the most important skills for writing apps for the web: you'll get a chance to practice responding to GET and POST requests, parsing JSON, authenticating users, and using a MySQL database.