Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return by reference in PHP

I tried Googling, tried PHP Documentation, searched Stack Overflow for an answer but couldn't find anything satisfactory. I was reading a book in which author have made use of Return by Reference but never explained what it is. The code used by the author is

function &getSchool() {     return $this->school; } 

Can someone explain in simple words with an example about this concept.

like image 321
Ibrahim Azhar Armar Avatar asked Sep 17 '11 14:09

Ibrahim Azhar Armar


People also ask

What is the use of return by reference?

A C++ program can be made easier to read and maintain by using references rather than pointers. A C++ function can return a reference in a similar way as it returns a pointer. When a function returns a reference, it returns an implicit pointer to its return value.

What is passing by reference in PHP?

Pass by reference In order to receive arguments by reference, variable used formal argument must be prefixed by & symbol. It makes reference to variables used for calling the function. Hence, result of swapping inside function will also be reflected in original variables that were passed.

What is return in PHP function?

The return keyword ends a function and, optionally, uses the result of an expression as the return value of the function. If return is used outside of a function, it stops PHP code in the file from running.

Are PHP arrays passed by reference?

With regards to your first question, the array is passed by reference UNLESS it is modified within the method / function you're calling. If you attempt to modify the array within the method / function, a copy of it is made first, and then only the copy is modified.


1 Answers

Suppose you have this class:

class Fruit {     private $color = "red";      public function getColor() {         return $this->color;     }      public function &getColorByRef() {         return $this->color;     } }  

The class has a private property and two methods that let you access it. One returns by value (default behavior) and the other by reference. The difference between the two is that:

  • When using the first method, you can make changes to the returned value and those changes will not be reflected inside the private property of Fruit because you are actually modifying a copy of the property's value.
  • When using the second method, you are in fact getting back an alias for Fruit::$color -- a different name by which you refer to the same variable. So if you do anything with it (including modifying its contents) you are in fact directly performing the same action on the value of the property.

Here's some code to test it:

echo "\nTEST RUN 1:\n\n"; $fruit = new Fruit; $color = $fruit->getColor(); echo "Fruit's color is $color\n";  $color = "green"; // does nothing, but bear with me $color = $fruit->getColor(); echo "Fruit's color is $color\n";   echo "\nTEST RUN 2:\n\n"; $fruit = new Fruit; $color = &$fruit->getColorByRef(); // also need to put & here echo "Fruit's color is $color\n";  $color = "green"; // now this changes the actual property of $fruit $color = $fruit->getColor(); echo "Fruit's color is $color\n";  

See it in action.

Warning: I feel obliged to mention that references, while they do have legitimate uses, are one of those features that should be used only rarely and only if you have carefully considered any alternatives first. Less experienced programmers tend to overuse references because they see that they can help them solve a particular problem without at the same time seeing the disadvantages of using references (as an advanced feature, its nuances are far from obvious).

like image 138
Jon Avatar answered Sep 19 '22 17:09

Jon