Re: convert_to_string_ex is not the solution.
| From: | Zeev Suraski | Date: | Fri, 17 Sep 1999 12:39:25 +0000 |
| Subject: | Re: convert_to_string_ex is not the solution. | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-4573@lists.php.net to get a copy of this message | ||
On Fri, 17 Sep 1999, Thies C. Arntzen wrote:
> zeev,
>
> script:
> <?
> $what = 1;
> var_dump($what);
> echo strlen(&$what)."\n";
> var_dump($what);
> ?>
>
> outputs:
> Content-Type: text/html
>
> int(1)
> 1
> string(1) "1"
>
> so strlen still does convert it's arg into a string if if the arg is
> passed by refernce.
As it should. It's consistent with the PHP 3.0 behavior (and PHP 4.0
behavior too) - if a variable is passed by reference, any modifications
you make to it should be reflected in the outside world. If you don't
want the function to affect your variables, don't pass them by reference.
If you want a plane of reference, internal functions should behave like
user functions as far as this is concerned. Consider the following
function:
function ord_next($n)
{
return ++$n;
}
Ok, a dumb function, but illustrative nonetheless. It's obvious that this
function would behave differently if you pass the argument by reference or
not. Same goes for internal functions.
Note that this has nothing to do with getParametersEx(). As I mentioned,
there's full consistency with the way PHP 3.0 and 4.0 behave. Keep
strlen() as it was, and try the following code (even on PHP 3.0):
$n = 5;
strlen(&$n);
print gettype($n);
You would get 'string'. This is exactly the reason that we added the
'allow pass by reference' flag in PHP 3.0, which would have better named
as 'allow implicit pass by reference', since nothing ever stops the user
from passing stuff by reference (that flag only tells PHP 3.0 if it may
pass stuff by reference even if the user didn't explicitly ask for that,
for performance reasons). As we said over a year ago, you're allowed to
use this flag only if your function uses the argument in a 'read-only'
mode, as in, doesn't modify them in ANY way, including type conversion.
You can understand from that that if the user passes arguments
by reference (explicitly) to a function that modifies its arguments, it
would modify them. With PHP 4.0, the performance increase from passing by
reference is gone (since passing any type of argument is only a matter of
increasing reference count), but everything else still applies.
We might want to add an argument type flag that prevents passing by
reference, but I'm not sure it's required.
Zeev
--
-----------------------------------------------------
Zeev Suraski <zeev@zend.com> http://www.zend.com/
For a PGP public key, finger bourbon@netvision.net.il