0

If I try this code :

<?php

class ref
{

    public $reff = "original ";

    public function &get_reff()
    {
        return $this->reff;
    }

    public function get_reff2()
    {
        return $this->reff;
    }
}

$thereffc = new ref;

$aa =& $thereffc->get_reff();

echo $aa;

$aa = " the changed value ";
echo $thereffc->get_reff(); // says "the changed value "
echo $thereffc->reff; // same thing
?>

Then returning by reference works and the value of the object property $reff gets changed as the variable $aa that references it changes too.

However, when I try this on a normal function that is not inside a class, it won't work !!

I tried this code :

<?php

function &foo()
{
    $param = " the first <br>";
    return $param;
}

$a = & foo();

$a = " the second <br>";

echo foo(); // just says "the first" !!!

it looks like the function foo() wont recognize it returns by reference and stubbornly returns what it wants !!!

Does returning by reference work only in OOP context ??

8
  • 3
    I'm really interested, why would you ever want that anyway? What ever compelled you to do such a thing? Commented Jul 30, 2013 at 16:29
  • madara i read about returning by reference and by the way i am reading a function in wordpress that uses that so i wanted to know what the heck that coder wanted to do by adding a & before the name of his function !!! Commented Jul 30, 2013 at 16:35
  • in the case of returning something large (Not just a string, but say a huge result set) if you return by reference, you pass the address of it back as opposed to copying the entire result into a new variable. it doesn't as you've assumed keep the variable inside function scope. Commented Jul 30, 2013 at 16:36
  • 2
    @hanachi it's WP, so I'd say the likelihood of the coder knowing what he wanted to do is close to 0% <scnr/>. There is very little reason to use references in your daily coding. Please see schlueters.de/blog/archives/125-Do-not-use-PHP-references.html Commented Jul 30, 2013 at 16:36
  • @hanachi: Hint, you should probably never do that. WP code is horrible, don't use it as a role model. Commented Jul 30, 2013 at 16:37

2 Answers 2

3

That is because a function's scope collapses when the function call completes and the function local reference to the variable is unset. Any subsequent calls to the function create a new $param variable.

Even if that where not the case in the function you are reassigning the variable to the first <br> with each invocation of the function.

If you want proof that the return by reference works use the static keyword to give the function variable a persistent state.

See this example

function &test(){
    static $param = "Hello\n";
    return $param;
}


$a = &test();
echo $a;
$a = "Goodbye\n";

echo test();

Echo's

Hello
Goodbye
Sign up to request clarification or add additional context in comments.

7 Comments

arigato madara ; yeah i know this !! then return by reference is a myth ?
No .. it's not a myth and you have proven it works in a situation where that would be sane behavior i.e. an Object that has persistent state.
i have proven that it is a reality in OOP inside a class but not in normal PHP code !
the guys in php.net doc should have made it clear enough ! right ?
See update.... you should reread the description of function scope here
|
0

Does returning by reference work only in OOP context ??

No. PHP makes no difference if that is a function or a class method, returning by reference always works.

That you ask indicates you might have not have understood fully what references in PHP are, which - as we all know - can happen. I suggest you read the whole topic in the PHP manual and at least two more sources by different authors. It's a complicated topic.

In your example, take care which reference you return here btw. You set $param to that value - always - when you call the function, so the function returns a reference to that newly set variable.

So this is more a question of variable scope you ask here:

2 Comments

thank you for the advice but i ll be greedy ! can you correct my function so that returning by reference works? i am telking about the simple function in php 4 just dont use static variables inside the function and not already referenced parameters ! I ll be thankfull !
There is nothing to work in your example. As written, writing the function that way is more you misunderstanding what variable scope is. Only using return by reference as well does not introduce a quantum shift that will make the workings of variable scope obsolete. As it's inherently not clear what you're trying to do, there is no code to provide. Also there is nothing to fix. You should be more greedy and demand a better understanding from yourself than looking for codes. A simple fix for example is to remove the return by reference. Then think again about your design.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.