Re: Passing functions to function by reference
| From: | Etienne Kneuss | Date: | Sat, 29 Apr 2006 09:34:12 +0000 |
| Subject: | Re: Passing functions to function by reference | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-23021@lists.php.net to get a copy of this message | ||
Hello,
I don't see the point using
func_taking_reference($quiet=some_func());
The expression $quiet=some_func() will not "return" $quiet itself. So
its not an alternative to $quiet=some_func(); func_taking_reference($quiet);
You don't avoid the E_STRICT here.
Regards
Rasmus Lerdorf a écrit :
> Brion Vibber wrote:
>> Rasmus Lerdorf wrote:
>>>> PHP 5.0 broke this. There was a fatal error on the array_shift that
>>>> only variables could be passed by reference. There was a good
>>>> argument for it. So, we started migrating our code.
>>>>
>>>> Well, seems this works in 5.1. So, my question is, was it an
>>>> intentional "fix" or will this break again and we need to continue to
>>>> watch for this?
>>> In 5.1 this now throws an E_STRICT instead of a warning. It is still a
>>> bad idea to pass a tempvar by reference, so yes, you should strive to
>>> write E_STRICT clean code.
>>
>> Stupid question: why is it a bad idea?
>
> Well, the original reason was because it caused weird and wonderful
> memory corruption in PHP 4.3.x, so if your code is ever going to run
> on that version you really need to not do that.
>
> It also tends to be a bug. Most functions that take an argument by
> reference do so for a reason. There are of course exceptions to this
> as you have pointed out and in those cases assuming your code will
> never run under PHP 4.3.x it can be valid. I would still say it is a
> better idea to something like:
>
> func_taking_reference($quiet=some_func()); // throw away ref mod
>
> and document the fact that you are intentionally throwing away the
> reference modification that func_taking_reference is making. That way
> you avoid the E_STRICT and make it very clear what you are doing.
>
> You could make the same argument for using undefined variables. Often
> that indicates a bug so the E_NOTICE can be really helpful, but other
> times it just gets in the way. I don't think that means we should do
> away with the E_NOTICE on that and I also don't think it means you
> shouldn't try to write E_NOTICE clean code.
>
> -Rasmus
>
--
Etienne Kneuss
http://www.colder.ch/
colder@php.net