Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Flourish using call-time pass-by-reference and PHP 5.4.

posted by cnicodeme 7 years ago


Since PHP 5.3 deprecating call-time pass-by-reference, and 5.4 throws a fatal error (source: http://www.php.net/manual/en/language.references.pass.php), I was wondering if it was planned to remove all those "&" in Flourish.

If not, why? Is there a particular reason?

Thanks for your reply.

Flourish does not use call time pass by reference, but rather normal pass by reference that is defined in method signatures. References are used to keep multiple copies of data structures intact, such as the identity map, so that if you load user 1 in two different scopes, you still are referencing the same mutated state of the data. Additionally references are used for in the JSON parsing, because it is the best way to handle the data traversal.

Flourish is tested on PHP 5.1, 5.2, 5.3 and 5.4. http://flourishlib.com/Tests shows the most recent results for all of the different environments.

posted by wbond 7 years ago

Thanks for the fast reply.

Something is bothering me though, as read on this page : http://www.php.net/manual/en/language.references.pass.php :

As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error.

If understood correctly, that means a function like foo(&$a) won't be accepted. Now, if I look at https://github.com/flourishlib/flourish-classes/blob/master/fStatement.php#L410 (for example), I see that exact same behavior :

public function executeQuery($result, $params, &$extra, $different) {}

So I expect a E_DEPRECATED in 5.3 and E_STRICT in 5.4.

But now, you send me the http://flourishlib.com/Tests link, which seems to indicate that there is no error for PHP 5.4 nor PHP 5.3 (in vm-arch.wbond.net). (note: I can't see vm-arch.wbond.net in https://github.com/flourishlib/flourish-tests-results/blob/master/results/01512d2a544a277d7b264b45a2c82a6b31a5701b.json ?).

So now I'm confused ...

Is there something I'm missing? between what PHP consider call-time pass-by-reference and what you do, like in the fStatement.php lib ?

Thanks for your help.

posted by cnicodeme 7 years ago

Right, so there are two kinds of pass by reference, those defined when the function or method is defined, or those that are defined when you call the function.

The example you linked to in the Flourish source is defining a method as having a argument passed by reference, meaning it won't make a copy of it if changes are made, but instead the original variable from the calling scope will be affected.

// This is ok!
function foo($var1, &$var2) {
    $var1 += 1;
    $var2 += $var1;
    return $var2;

Call-time pass-by-reference is when you force a function or method to accept a variable by reference even if it is not expecting it. This is generally bad, because the function may not know it has a reference and may do terrible things to your variable. However, usually if you are doing it, it is for a reason. It looks like:

$my_var = 3;
$my_var2 = 0;

// Normal call
foo($my_var, $my_var2);
print $my_var; // still 3!
print $my_var2; // 4

// Call-time pass by reference, notice the & symbol for the first var, when calling the function
foo(&$my_var1, $my_var2);
print $my_var; // Uh oh, now 4!
print $my_var2; // 8

I hope this helps some.

posted by wbond 7 years ago

Yep! It make more sense now. I wasn't aware of that difference. I thought it was either allowed to use reference or not. Not in a granular way like this.

Thanks for pointing this out :)

posted by cnicodeme 7 years ago