ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> php-dev
php-dev
Re: [PHP-DEV] allow_call_time_pass_reference
by Todd Ruth other posts by this author
May 8 2008 1:08PM messages near this date
Re: [PHP-DEV] allow_call_time_pass_reference | Re: [PHP-DEV] allow_call_time_pass_reference
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
Thread:
Steph Fox
Hector Santos
Steph Fox
Steph Fox
Steph Fox
Hector Santos
Hannes Magnusson
Todd Ruth
Hannes Magnusson
Todd Ruth
Steph Fox
Steph Fox
Stanislav Malyshev

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved