Home » Php » PHP – Make reference parameter optional?

PHP – Make reference parameter optional?

Posted by: admin November 30, 2017 Leave a comment

Questions:

Say I’ve defined a function in PHP, and the last parameter is passed by reference. Is there any way I can make that optional? How can I tell if it’s set?

I’ve never worked with pass-by-reference in PHP, so there may be a goofy mistake below, but here’s an example:

$foo;
function bar($var1,&$reference)
{
    if(isset($reference)) do_stuff();
    else return FALSE;
}

bar("variable");//reference is not set
bar("variable",$foo);//reference is set
Answers:

Taken from PHP official manual:

NULL can be used as default value, but it can not be passed from outside

<?php

function foo(&$a = NULL) {
    if ($a === NULL) {
        echo "NULL\n";
    } else {
        echo "$a\n";
    }
}

foo(); // "NULL"

foo($uninitialized_var); // "NULL"

$var = "hello world";
foo($var); // "hello world"

foo(5); // Produces an error

foo(NULL); // Produces an error

?>

Questions:
Answers:

You can make argument optional by giving them a default value:

function bar($var1, &$reference = null)
{
    if($reference !== null) do_stuff();
    else return FALSE;
}

However please note that passing by reference is bad practice in general. If suddenly my value of $foo is changed I have to find out why that is only to find out that it is passed by reference. So please only use that when you have a valid use case (and trust me most aren’t).

Also note that if $reference is supposed to be an object, you probably don’t have to (and shouldn’t) pass it by reference.

Also currently your function is returning different types of values. When The reference is passed it returns null and otherwise false.

Questions:
Answers:

In reaction to @Martin Perry’s post, you can distinguish between

foo(); // "NULL"

and

foo($uninitialized_var); // "NULL"

using func_num_args()

Questions:
Answers:

you can set default values

$foo;
function bar($var1,&$reference = false)
{
    if($reference != false) do_stuff();
     else return FALSE;
}

bar("variable");//reference is not set
bar("variable",$foo);//reference is set

Questions:
Answers:

try

use function bar($var1,&$reference=null)
{
    if(isset($reference)) do_stuff();
    else return FALSE;
}

if you pass value to &$reference it will take that value. or else null.

Questions:
Answers:

Two options:

Declare a default value, which is outside of the regular value range, for example -1

function bar($var1,&$reference = -1)
{
    if($reference !== -1) do_stuff();
    else return FALSE;
}

Do not declare the param and check func_get_args() to see if it was passed:

function bar($var1)
{
    if(count(func_get_args()) > 1) do_stuff();
    else return FALSE;
}

But note, the second approach will trigger a deprecated warning: Call-time pass-by-reference has been deprecated;

Questions:
Answers:

The following code snippet illustrates that a reference is just an alias for a variable name:

<?php
function set_variable(&$var, $val)
{
  $var = $val;
}

set_variable($b, 2);
echo $b; // 2
?>

This article by Larry Ullman explains the basics of passing variable by reference vs passing by value.

What’s interesting is that at the end of Larry’s article is a link to another article that goes in depth to explain the underpinnings of references and variables in PHP.

All in all, when making reference parameter optional by giving a default value, PHP ‘creates’ a new variable which holds the default value in case we do not provide a value for the optional parameter. But, in case we do provide one, we must provide a variable for which the reference parameter is but an alias.

Leave a Reply

Your email address will not be published. Required fields are marked *