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.
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.
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.
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.
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.
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:
Fruit
because you are actually modifying a copy of the property's value.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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With