Re: Tidying code for PHP5.0.5/PHP4.4.0
| From: | Jochem Maas | Date: | Tue, 20 Sep 2005 09:13:51 +0000 |
| Subject: | Re: Tidying code for PHP5.0.5/PHP4.4.0 | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-222738@lists.php.net to get a copy of this message | ||
Jasper Bryant-Greene wrote:
Lester Caine wrote:wtf, Im now officially confused (before I suffered unofficially :-) if foo() expects one args by reference then why is doing: foo($a = 5); ..wrong? I always thought that the expression (in this form) 'returns' the variable? or does it need to be:? foo( ($a = 5) ); in fact I'm pretty sure that an example (like this) of a fix for the 'bad coding' practice that is now throwing notices/errors was given on internals-php quite recently. I can understand that the following are wrong: foo(5); // not sure this is wrong. foo(bar()); but how is: foo(($a = 5)); different from?: $a = 5; foo($a);I suppose the REAL questions was - "Why was using the function in this way a 'bad practice', is there any way that it could be made a 'good practice' since the intent is so obvious?" I understand the new checks, but I don't see that the original was particularly 'bad' - only that under some conditions it might cause a problem?Basically, in PHP, a reference (such as what key() takes as a parameter [1]) can only point to an actual variable, not directly to the result of a function. So you have to assign the output of the function to a variable first. From the PHP manual [2]: | the following examples of passing by reference are invalid: | | foo(bar()); // Produces fatal error since PHP 5.1.0 | foo($a = 5); // Expression, not variable
| foo(5); // Produces fatal error Whether this is the optimal behaviour has been the subject of recent debate on this list [3]. Rasmus states that the current behaviour of throwing a fatal error will be changed to a notice. [1] http://php.net/key [2] http://php.net/language.references.pass [3] http://marc.theaimsgroup.com/?l=php-general&m=112689425109173&w=2