ID: 38626 User updated by: php at swapo dot de Reported By: php at swapo dot de Status: Wont fix Bug Type: Feature/Change Request Operating System: all PHP Version: 5.1.5 New Comment:
And I'd like to know a reason for this ...uhm... uncommon behaviour ;-) "Because it is so" doesn't seem like a good reason to me. Especially as it wasn't a problem in the past (although it may not have beenn meant that way) and PHP5's object references work another way. Seems a little unformed to me so far. Previous Comments: ------------------------------------------------------------------------ [2006-08-28 14:44:56] [EMAIL PROTECTED] It's not an "internal flaw" - it's how it is supposed to work. A reference works on a *variable* not only on its contents. ------------------------------------------------------------------------ [2006-08-28 14:36:37] php at swapo dot de I understand the reason as it is in the manual too. That's why I submitted this as a change request. If you can give me a good reason (aside from php's internal "flaws") why my example is bad I'll accept that. In my understanding a reference should be on the content not on a variable name. I guess that's the way it's done with objects since PHP5 and the way references are handled in other languages (no one would mind "someMethod(new SomeClass())" or "someMethod(getSomeObject())"). If this isn't possible with the scripting engine it'd still be enough to keep the old behaviour and silently ignore the & in that cases. Backwards compatibility can't be the reason too as scripts that worked in the past generate errors now. BTW: That's the way I came across this issue: By migrating from 5.0 to 5.1. Adding either two useless statments ("$var = some_function();" and "unset($var);" or an error suppressing "@" seems uneeded complicated to me. ------------------------------------------------------------------------ [2006-08-28 13:33:23] [EMAIL PROTECTED] >$extension = @array_pop(explode('.', $filename)); explode(..) is not a variable, hence it can't be passed by reference. ------------------------------------------------------------------------ [2006-08-28 12:43:48] php at swapo dot de Description: ------------ The behaviour of pass by reference is a little odd. As stated here (http://www.php.net/manual/en/language.references.pass.php) passing by reference works only if you use a variable as a function argument. This is also the case with builtin functions like array_pop but I can think of many situations when it makes sense to use a function's return value as an argument to another function. A common example (perhaps not the best though) is to get a file extension by combining explode and array_pop. This wasn't a problem until PHP 5.1 and I think it shouldn't be now. Reproduce code: --------------- // doesn't work $extension = array_pop(explode('.', $filename)); // works $parts = explode('.', $filename); $extension = array_pop($parts); unset($parts); // surpressing the error message works too $extension = @array_pop(explode('.', $filename)); Expected result: ---------------- All examples should give the same result. Actual result: -------------- The first example results in an error message as the argument for array_pop must be a variable: Strict Standards: Only variables should be passed by reference ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=38626&edit=1