Video: Referencing objectsIn 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
Copying AddressBusiness reference; $address_business _copy = $address_business.
Viewers: in countries Watching now:
Whether you're enhancing or optimizing existing code or just starting from scratch, there's never a better time to start integrating object-oriented design techniques. This course shows how to integrate the principles of object-oriented programming into the build of a PHP-driven web page or application. After an overview of what objects and classes are and why they should be used, author Jon Peck dives into creating and instantiating objects, then defining the class relationships and interactions that will form the basis of your coding arsenal. The course also shows how to leverage PHP objects and implement design patterns, and looks at steps you can take to continue adding to your programming tool belt.
- 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
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.
Find answers to the most frequently asked questions about Object-Oriented Programming with PHP .
Here are the FAQs that matched your search "" :
- Q: I'm not seeing warnings or errors in my environment like the video; why not?
- A: Your PHP configuration is probably configured not to show them to you. This is often true on commercial web hosts and is often the default. Fortunately, there are multiple ways of resolving this.
The easiest way would be to explicitly enable error reporting at the top of the PHP script you wish to debug.
error_reporting(E_ALL | E_STRICT);
Alternatively, if you have access to your php.ini file and you want to always have error reporting on, change error_reporting = to a development friendly value of
error_reporting = E_ALL | E_STRICT
then change display_errors = to
display_errors = On
Finally, if access to the php.ini file is not available, you can add the following directives to your .htaccess or VirtualHost configuration for Apache:
php_value error_reporting 32767
php_value display_errors 1
If you would like a development optimized development environment like the one utilized in this course, see Up and Running with Linux for PHP Developers, here in the lynda.com online training library.
Q: This course was updated on 4/10/2013. What changed?
A: The author rerecorded some of the tutorials to add more background information and better graphics.
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.