On Thu, 2008-05-08 at 20:28 +0100, Steph Fox wrote:
> ...
> Does anyone have a good reason for keeping it switched on by default in PHP 
> 5.3? Like, would switching it off by default break a lot of existing code, 
> given that most users are a bit beyond PHP 3 now?

Well, I can at least comment on how it is used in the code that
I inherited.  First it must be noted that the following throws 
a fatal error:

function f(&$x=5) { ....

There are functions in our application that will use information
if it is available and update the information if it is provided,
but don't absolutely require such information in the first place.
I don't know of another clean way to do that besides call-time
pass-by-reference.  Here is a useless example of the functionality.  
(This is an example, and only an example.  In the event of real 
code, something useful would be accomplished. ;) )

<?php
// $x is sometimes received by reference
function f($x=5) {
   if ($x < 10) {
      print "blue\n";
      $x += 10;
      $retval = true;
   } else {
      print "green\n";
      $x += 20;
      $retval = false;
   }
   return $retval;
}

f();
f(10); // In this case info is provided, but the update is discarded
$y=5;
f(&$y);
f(&$y);
?>

Without call-time pass-by-reference, that becomes:

function g(&$x) {
   if ($x === null) {
      $x = 5;
   }
   if ($x < 10) {
      print "blue\n";
      $x += 10;
      $retval = true;
   } else {
      print "green\n";
      $x += 20;
      $retval = false;
   }
   return $retval;
}

$y = null;
g($y);
$y = 10;
g($y);
$y=5;
g($y);
g($y);

So, without call-time pass-by-reference, "if" blocks would be
needed to set defaults and temp variables would be needed
anywhere an rvalue is used as an argument.  It's true that
the former has more opportunities for design error and without
the comment about $x being received by reference there could
be confusion, but going back and changing all that code is not 
desirable.  "Easier to read" is always a tricky argument, but
I'm sure many would find the first code above easier to read.

- Todd

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to