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:

This type of code is used in a few places, so I'd like a little help converting it to 'good code' under the new rules ;)

They're not "new rules". PHP is just warning you where it didn't before. It was still bad coding practice before.

Since it is not my own code ...
The problem is that little problems like this HAVE been duplicated and no one has complained before ;)
Now we have to go through every library and eliminate them ...

Get the key from an array ( fails because key(&array) )
----
if( $pId == key( $this->getFunc() ) ) {

In getFunc()
----
return ( $this->getAssoc("select Id, Content from database...");

Current fix is just to create the array and use that
$keyarray = $this->getFunc();
if( $pGroupId == key( $keyarray ) ) {

This works fine, but is there a 'proper' way to do it now that what looked tidy originally fails with an error?

That is the 'proper' way, since you are using key() exactly as it is intended to be used, although you could avoid the reference like this:

$keys = array_keys( $this->getFunc() );
if( $pGroupId = $keys[0] ) {

Actually the proper way would be to get each function to produce a single result - but I'm not going to re-code everything :)

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?

--
Lester Caine
-----------------------------
L.S.Caine Electronic Services

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