APD Toronto
asked on
By Reference Parameters
Hi Experts,
Does PHP support passing parameters by reference, where you will have a function call like my_function($var1, $var2) and my_function will have the ability to change var1, var2, which will be seen by the calling code. For example:
if this possible in PHP, and more so in a class public static function? If not, how can I accomplish this?
Does PHP support passing parameters by reference, where you will have a function call like my_function($var1, $var2) and my_function will have the ability to change var1, var2, which will be seen by the calling code. For example:
$var1 = 1;
$var2 = 2;
my_function($var1, $var2);
$var3 = $var1 + $var2 //would equal 5
my_function($var1, $var2){
$var1 = 2;
$var2 = 3;
}
if this possible in PHP, and more so in a class public static function? If not, how can I accomplish this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here's simple working demo of your code, both by value and by reference.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Passing methods</title>
</head>
<body>
<h1>Passing methods</h1>
<h2>By value</h2>
<?php
$var1 = 1;
$var2 = 2;
my_function($var1, $var2);
$var3 = $var1 + $var2; //would equal 5
echo '$var3 = '.$var3.'<br>';
function my_function($var11, $var22) {
$var11 = 2;
$var22 = 3;
}
?>
<h2>By reference</h2>
<?php
$var1a = 1;
$var2a = 2;
my_functiona($var1a, $var2a);
$var3a = $var1a + $var2a; //would equal 5
echo '$var3a = '.$var3a.'<br>';
function my_functiona(&$var11a, &$var22a){
$var11a = 2;
$var22a = 3;
}
?>
</body>
</html>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yep even the experts refer to the good manual from time to timeNo, ALL the time. Why try to remember everything when I can just look it up? And Ray's demo code always has links to the appropriate pages for the functions he is using in his demos.
This is a commonly asked question, and we have an article here that shows the difference between passing by copy and passing by reference.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12310-PHP-Variables-and-References.html
* This is a term of art in object-oriented design.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12310-PHP-Variables-and-References.html
in a class public static function?After you read the article, you can just try it if you want. But I would not do that - instead return a value-object* and use an explicit assignment statement. You code will be clearer (easier to read and understand the intent) and there is no penalty in performance.
* This is a term of art in object-oriented design.
No, ALL the time
Ok agreed - at least a few times a week :)
What you have to watch out for is exceptions. Some functions require references and others require values.
http://php.net/ is always your friend in things like this.