Re: Array dereference on scalar / null values

From: Date: Tue, 18 Mar 2014 06:17:18 +0000
Subject: Re: Array dereference on scalar / null values
Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Hi internals,

I've done some bug report gathering on essentially the following behaviour:

$a = 123; var_dump($a[1]); // "NULL"
$b = true; var_dump($b[1]); // "NULL"
$f = fopen('file', 'r'); var_dump($f[1]); // "NULL"
$n = null; var_dump($n[1]); // "NULL"

Bug reports:

https://bugs.php.net/bug.php?id=37676
https://bugs.php.net/bug.php?id=40692
https://bugs.php.net/bug.php?id=62769
https://bugs.php.net/bug.php?id=64194
https://bugs.php.net/bug.php?id=65484

From the comments it seems that there's some disagreement within the group,
for example:

Nikita: "I would tentatively classify this as a bug."
Tony: "Reclassified as feature request.."
Xinchen: "there was a similar bug report of this behavior, and I seems made
a fix for it."
Ilia: "this is not a bug." x 2
Joe: "it is the only reasonable thing _to expect_"

From my understanding, developers are expecting to see notices when this
happens, something like "Cannot use integer/boolean/resource/null as array
in %s on line %d"; I think they're okay with the fact that the end result
will still be "NULL", though.

The list() operator has since complicated matters; if we would indeed
introduce notices as requested, the following code would cause issues:

$array = [1, 2, 3];
while (list($key, $value) = each($array)) {
    echo "$key = $value\n";
}

The return value of each() - as it has reached the end of an array - is
false, so the above code would show two notices. Unfortunately, we must
assume that all code in the manual are actually used and so it would surely
break stuff when introduced.

We could mitigate this issue by having each() return null instead and
introduce notices for all other scalars, but this code is not unlikely:

while (($data = each($array)) !== false) {
    echo "${data[0]} = ${data[1]}\n";
}

So my question is, how should we handle this? Close all bug reports and add
something to the documentation? Allow array dereference only on booleans
*and* null?


Best,
  Jack


Thread (3 messages)

« previous php.internals (#73258) next »