views:

210

answers:

3

Hello There,

In the minutes php6 developer meeting, i came across a point where it said that call-time-pass-by-reference is no longer there in PHP6. For example, following both are incorrect for PHP6:

<?php
$foo =& new StdClass();
?>

<?php
function &foo()
{
    return new StdClass();
}

$f = foo();
?>

If we can't use something like this in PHP6:

$foo =& new StdClass();

What is the alternative to that, is there any way to mimic that?

EDIT:

Ans what about variables in PHP6, can we do that for variables eg:

$this->data =& $_SESSION;
+3  A: 

You don't pass parameters by reference, but you still can/must declare your function/method as receiving parameters by reference.

i.e., you don't do this (passing parameters by reference) :

my_function(& $my_var);

function my_function($a) {
    // ...
}

But you can do this (declaring the function as receiving parameters by reference) :

my_function($my_var);

function my_function(& $a) {
    // ...
}


And... The code examples you gave are not related to call-time-pass-by-reference, but are related to return-by-reference.

For that second thing, what if you just remove the & ? The instance of the object that's been created inside the function will be returned, and you'll still be able to work with it, won't you ?

Pascal MARTIN
thanks for your answer, plz see my edit below question, thanks
Sarfraz
also i am not talking about functions, i am talking about call-time-pass-by-reference.
Sarfraz
Call-time pass-by-reference *is* to do with functions. `my_function(` is passing `$a` to `my_function` by reference at call-time, rather than at declare-time.
Dave
ok thanks for clarifying Dave...
Sarfraz
@Sarfraz Ahmed : hu... call-time-by-reference is about functions and methods : it's the passing of parameters to functions/methods by reference when calling them, instead of declaring them as receiving parameters by reference ;; it has nothing to do with your edit, which is just/only assignment by reference
Pascal MARTIN
@Pascal: is that assignment by reference correct and not deprecated/removed from php6?
Sarfraz
To be sure, I would say "try" ; but I don't think it (assigment of variables by reference) actually is deprecated. Still note that PHP 6 is not there yet, and many things can still change (there have been talks about PHP 6 at least those past 2 years, and it still isnt in alpha yet)
Pascal MARTIN
ok thanks for clarifying martin
Sarfraz
+4  A: 

Objects are always passed by reference since PHP5, so this:

$foo =& new StdClass();

is the same as:

$foo = new StdClass();

.

frunsi
thanks for your answer, plz see my edit below question, thanks
Sarfraz
+1  A: 

Ans what about variables in PHP6, can we do that for variables eg: $this->data =& $_SESSION;

I see no reason why not -- PHP will not be removing references, as they are far too useful. There is no other way to create a reference to a variable.

Call-time pass-by-reference has been deprecated for a while. It just means that instead of doing this:

function foo($a) { return ++$a; }
foo(&$my_a);

You would do this:

function foo(&$a) { return ++$a; }
foo($my_a);

This leads to a much cleaner and easier-to-understand programming style, and it also ensures that variables are always referenced when you expect, and not when you don't.

Dealing with objects is a special case -- they are always passed by reference, and the only way to simulate pass-by-value is to use clone:

$a = new StdClass();
$a->foo = 'bar';
$b = $a;
$a->foo = 'qux';
// $b->foo is 'qux' too

$a = new StdClass();
$a->foo = 'bar';
$b = clone $a;
$a->foo = 'qux';
// $a->foo is 'qux' but $b->foo is 'bar'

Hope that helps!

Dave