New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Referencing objects

From: Object-Oriented Programming with PHP

Video: Referencing objects

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

Copying AddressBusiness reference; $address_business _copy = $address_business.

Referencing objects

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

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

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.

Show transcript

This video is part of

Image for Object-Oriented Programming with PHP
Object-Oriented Programming with PHP

37 video lessons · 17424 viewers

Jon Peck
Author

 
Expand all | Collapse all
  1. 4m 10s
    1. Welcome
      39s
    2. Exercise files
      2m 16s
    3. What you should know
      1m 15s
  2. 7m 47s
    1. What is an object?
      2m 6s
    2. What is a class?
      2m 7s
    3. Why should you use object-oriented programming?
      2m 14s
    4. The history of object-oriented PHP
      1m 20s
  3. 16m 40s
    1. Defining a class
      2m 58s
    2. Defining class properties
      3m 26s
    3. Creating a method and exploring object context with $this
      2m 50s
    4. Instantiating an object and accessing its contents
      3m 19s
    5. Specifying the visibility scope
      4m 7s
  4. 15m 51s
    1. What is a magic method, and do I need one?
      2m 23s
    2. Overloading property access
      6m 37s
    3. Customizing object construction
      4m 34s
    4. Standardizing object rendering as a string
      2m 17s
  5. 20m 54s
    1. Using the static keyword
      3m 36s
    2. Leveraging scope resolution operators
      1m 10s
    3. Setting constant values
      2m 47s
    4. Implementing static methods
      5m 43s
    5. Creating a database class
      7m 38s
  6. 26m 19s
    1. Extending your class
      6m 8s
    2. Abstracting classes
      5m 57s
    3. Sharing interfaces using polymorphism
      4m 39s
    4. Overriding methods, properties, and constants
      3m 25s
    5. Cloning and comparing objects
      2m 51s
    6. Referencing objects
      3m 19s
  7. 14m 52s
    1. Leveraging standard class objects
      2m 42s
    2. Retrieving objects from the database
      5m 41s
    3. Error handling with exceptions
      2m 6s
    4. Customizing PHP exceptions
      4m 23s
  8. 8m 2s
    1. Identifying the singleton pattern
      1m 42s
    2. Using the factory method pattern
      1m 51s
    3. Implementing a strategy pattern
      4m 29s
  9. 2m 57s
    1. Looking forward to namespaces
      47s
    2. Next steps
      1m 6s
    3. Goodbye
      1m 4s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.