Jump to content

Return by reference


doebaladoo

Recommended Posts

I was thought that (when returning by reference) you should always add a & when assigning the return value. For instance:

 

function & a () { }
$a = & a();

 

and not:

 

$a = a();

 

The follow works hower (without the added & ):

 

class a {
	public $a = 1;
}

class b {
	static $var;

	public static function & gvar($i) {
		if(!(isset(b::$var[$i]))) {
			b::$var[$i] = new a;
		}

		return b::$var[$i];
	}
}

$b = new b;
$a = b::gvar(1);
$a->a = 14;

$c = b::gvar(1);
echo $c->a; // 14

 

Notice the

 

$a = b::gvar(1);

 

without the &.

 

Am I missing something???

Link to comment
Share on other sites

Daniels, I think he knows that.

 

doebaladoo, as of php5 all objects are passed by reference (they're instantiated on the heap if that means anything to you), so even specifying that the method returns a reference is not needed.

 

Try removing the & from the function prototype and you should still get the same result.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.