Cannot pass parameter null by reference

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Cannot pass parameter null by reference

Post by Ollie Saunders »

I have this function:

Code: Select all

public function setSource(&$r_source)
{
    if ($r_source === null) {
        switch ($_SERVER['REQUEST_METHOD']) {
            case 'POST':
                $this->_r_source =& $_POST;
                break;
            case 'GET':
                $this->_r_source =& $_GET;
            default:
                // REQUEST_METHOD is unreliable and use POST
                $this->_r_source =& $_POST;
        }
    } else if (is_array($r_source)) {
       $this->_r_source =& $r_source;
    } else {
        $errMsg = 'Source must be an array or null; ' . gettype($r_source) . ' given';
        throw new OF_Exception($errMsg, OF_Exception::TYPE_SCALAR);
    }
}
As you can see I'd like to be able to accept an array by reference or null. But you can't do that because:

Fatal error: Cannot pass parameter 1 by reference in /osis/lib/code/php/OF/Array.php on line 48

Other than two separate parameters is there anyway you can solve this?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You can't do:

Code: Select all

setSource(null);
But you can do:

Code: Select all

$x = null;
setSource($x);
You can't reference a constant.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

but there is no way to allow setSource(null);?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

not as reference.

as single line: setSource($x=null);
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

PHP5 supports this syntax:

Code: Select all

public function setSource(&$foo=null)
{
   //
}
But you can't explicitly pass null as a reference. PHP4 doesn't support the above.

If you think about it, it makes sense really... what would a null reference point to?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

volka wrote:not as reference.

as single line: setSource($x=null);
I guess that will have to do. Thanks guys.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

ole wrote:
volka wrote:not as reference.

as single line: setSource($x=null);
I guess that will have to do. Thanks guys.
The other approach is a bit cleaner in that setSource() without any args does what you need :)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Oh wow thanks for posting again. I missed that.
Post Reply