Re: Tidying code for PHP5.0.5/PHP4.4.0

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jasper Bryant-Greene wrote:
Lester Caine wrote:

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

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);


| 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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux