What does this signature mean (&) in PHP?
Asked Answered
E

3

10

Consider:

public function & get($name, $default = null)

Why &?

Endodontist answered 19/3, 2010 at 14:47 Comment(1)
possible duplicate of Reference - What does this symbol mean in PHP?Hypabyssal
H
11

In PHP's syntax, this means that the function returns a reference instead of a value. For example:

<?php
    $foo = 'foo';

    function & get_foo_ref ()
    {
        global $foo;
        return $foo;
    }

    // Get the reference to variable $foo stored into $bar
    $bar = & get_foo_ref();
    $bar = 'bar';

    echo $foo; // Outputs 'bar', since $bar references to $foo.
?>

In the above example, removing the & from the function declaration would make the $foo variable still contain 'foo', since only the value, not the reference was returned from the function.

This was used more often in PHP4, because it did not pass objects by their reference and cloned them instead. Because of this, object variables had to be passed by reference to avoid unwanted cloning. This is no longer the case in PHP5 and references should not be used for this purpose.

However, functions that return references are not completely useless either (or bad practice, when not used for replacing object references).

For example, personally I've used them when creating a script that passes a "path" to an function, which returns reference to variable in that path allowing me to set value to it and read the value. Due to the recursive nature of the function, returning the reference was needed.

By rithiur

Hales answered 19/3, 2010 at 14:54 Comment(4)
+1 for pragmatic in-depth explanation that also doubles as evidence that SO's rep system is not all that fairKenyatta
$bar = & get_foo_ref(); ,here & is not necessaryEndodontist
@Manos Dilaverakis:I think sharing good stuff would never be a fault.Hales
@user198729:In this example it seems to be necessary.Hales
K
11

It means it returns a reference to the result as opposed to a copy of it.

Returning References in PHP

Kenyatta answered 19/3, 2010 at 14:50 Comment(1)
Just a sidenote, this is almost never beneficial. Objects are passsed around by reference. The only thing this COULD help with is arrays (so they aren't copied), but the benefit is minimal, if existant at all.Ruttger
H
11

In PHP's syntax, this means that the function returns a reference instead of a value. For example:

<?php
    $foo = 'foo';

    function & get_foo_ref ()
    {
        global $foo;
        return $foo;
    }

    // Get the reference to variable $foo stored into $bar
    $bar = & get_foo_ref();
    $bar = 'bar';

    echo $foo; // Outputs 'bar', since $bar references to $foo.
?>

In the above example, removing the & from the function declaration would make the $foo variable still contain 'foo', since only the value, not the reference was returned from the function.

This was used more often in PHP4, because it did not pass objects by their reference and cloned them instead. Because of this, object variables had to be passed by reference to avoid unwanted cloning. This is no longer the case in PHP5 and references should not be used for this purpose.

However, functions that return references are not completely useless either (or bad practice, when not used for replacing object references).

For example, personally I've used them when creating a script that passes a "path" to an function, which returns reference to variable in that path allowing me to set value to it and read the value. Due to the recursive nature of the function, returning the reference was needed.

By rithiur

Hales answered 19/3, 2010 at 14:54 Comment(4)
+1 for pragmatic in-depth explanation that also doubles as evidence that SO's rep system is not all that fairKenyatta
$bar = & get_foo_ref(); ,here & is not necessaryEndodontist
@Manos Dilaverakis:I think sharing good stuff would never be a fault.Hales
@user198729:In this example it seems to be necessary.Hales
C
0

PHP's pass by reference metacharacter. See Passing by Reference

Cement answered 19/3, 2010 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.