Join Jon Peck for an in-depth discussion in this video Referencing objects, part of Object-Oriented Programming with PHP.
- View Offline
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 <h2>Copying AddressBusiness 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 <h2> 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 '<br/>$address_business is class ' . get_class($address_business). You can also use the function instanceof to make logical decisions. echo '<br/> 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.
- Historical overview of object-oriented PHP
- Defining classes
- Creating a method/object context with $this
- Accessing classes without instantiation
- Creating a database class
- Extending and abstracting classes
- Cloning and comparing objects
- Error handling with exceptions
- Implementing design patterns, such as the factory and strategy patterns