From the course: PHP: Object-Oriented Programming

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Assignment by reference

Assignment by reference

- [Instructor] Now that we've learned about cloning, we should pause and take a moment to talk about assignment by reference and a key difference that exists in object-oriented programming. Normally in PHP, values are copied whenever we make an assignment. So we have a variable a, we set it equal to one, and we have another variable b, we set it equal to a. We would expect b to be equal to one. And if we change the value of a to something else, like two, we would not expect the value of b to change. It should still be equal to the value one. That is normal assignment. PHP also offers us the ability to do assignment by reference. We would set a equal to one, but now when we're assigning a to b, we can add an ampersand in front of the variable a or you can do it right after the equal sign. The effect of this is to tell PHP don't assign the value to b, assign a reference to the same thing that a points to. So now a and…

Contents