|
Posted by Rasmus Lerdorf on 09/20/05 18:27
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 ;)
>
> 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?
There is nothing wrong with that code. key() and current() don't need
to take a reference and this has been fixed in CVS and will be in the
next release. It was overlooked in the past because we didn't have any
sort of warning about discarded references. We have also fixed things
such that discarded references in other circumstances such as:
array_pop, array_shift, end(), etc. will only throw an E_STRICT in PHP
5.x. E_STRICT is the new super-pendantic warning level that is disabled
by default but can be turned on to help you track down potential sources
of errors such as doing:
sort($this->getArray());
If you have forgotten to make the getArray() method return its array by
reference, then that sort call will do absolutely nothing and it can
often be really hard to find a bug like that.
-Rasmus
[Back to original message]
|