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. Show
Recap: Review the code from the previous article. php-youtube-tutorials/65 Final KeywordCode for the PHP YouTube tutorials. Contribute to dinocajic/php-youtube-tutorials development by creating an…github.com PHP — P65: Final KeywordThe 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.
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-tutorialsCode for the PHP YouTube tutorials.github.com 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. |