Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In PHP, a reference is an alias, meaning two different variables can write to the same value.
There is a good chance you've seen this in procedural code before.
Referenced objects are a bit different however, in that the object variables do
not contain the actual object itself, only the internal object identifier, which
is behind the scenes in PHP.
This is one of the major changes from PHP 4, which greatly improved memory
usage and performance.
To demonstrate this, make a copy of the variable for AddressBusiness.
Add the following block to the end of the demo: echo
reference; $address_business _copy = $address_business.
Copy the logic from the last demo. Use triple equals (===) to determine if it's an exact copy. address_business_copy is or is not a copy of address_business. Next, we're going to set address_ business_copy as a new address_park. echo setting_address_business as a new address_park. $address_business = new address_park. And, we'll paste the same line.
Finally, use the get_class function to get the name of an object's class. echo
$address_business is class ' . get_class($address_business). You can also use the function instanceof to make logical decisions. echo '
address business copy is address_business_copy instanceof AddressBusiness and $address_business.
This last line will determine whether or not address_business_copy is an instance of address_business. Save, and view the result in your browser. When you copy by identifier, you end up with a copy of the object, as you would expect. To demonstrate this, I am going to make a copy by reference. Return to the demo code, and add the reference symbol to the assignment, after address_business_copy. Save, and rerun the demo. This time, the object comparison makes it clear that actions taken on the core object affect the referenced object.
In this chapter, the focus has been on class relationships and interactions. We started by extending the Address class with address type specific subclasses, and enabled autoloading to deal with all the different class files. We then abstracted the Address class and methods, and then created a shared interface to add structure. We overrode methods and properties, and learned how to override constants, as well. We made copies of objects, and compared them to one another, and implemented cloning behaviors. Finally, we experimented with referencing objects.
In the next chapter, I'm going to demonstrate objects that are ready built into PHP, including the standard class and exceptions.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98192 Viewers
61 Video lessons · 85490 Viewers
71 Video lessons · 69457 Viewers
56 Video lessons · 101801 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Your file was successfully uploaded.