Hi everybody!
To my surprise, I noticed today that an array access on a null value returns null without any warning whatsoever.
Accessing the property of a null value gives you a notice, while calling a function on that null value gives you a fatal error.
This is pretty inconsistent. IMO all of these should be fatal errors, although I realize that might be a bit drastic for PHP 8.
We should at least give the user a notice when using array access on a null value.
Any thoughts?
Regards,
Ilija
Am 20.09.2017 um 19:06 schrieb ilija.tovilo@me.com:
Hi everybody!
To my surprise, I noticed today that an array access on a
nullvalue returnsnullwithout any warning whatsoever.
Accessing the property of anullvalue gives you a notice, while calling a function on thatnullvalue gives you a fatal error.This is pretty inconsistent. IMO all of these should be fatal errors, although I realize that might be a bit drastic for PHP 8.
We should at least give the user a notice when using array access on anullvalue.Any thoughts?
wouldn't that break code like below which is IMHO the best usecase of
the PHP 5.6 feature that you can directly access a array field of a
function result and the type-casting encsues that the return-type is
always int
function GetSKatMaxSort(int $archive=0): int
{
return (int)mysqli_fetch_row($this->db->query("select SQL_NO_CACHE
max(ssort) from {$this->db->table} where sarchive=$archive", 1))[0];
}
I realized after sending the initial E-Mail that there might be more breaking changes than I thought, like the one you suggested.
The same thing actually happens for bools, ints and floats:
https://3v4l.org/jvVbO
I wouldn’t depend on this behaviour since you’d get errors depending on what is returned, like an empty array, a string, an object or resource.
Regards
Am 20.09.2017 um 19:06 schrieb ilija.tovilo@me.com:
Hi everybody!
To my surprise, I noticed today that an array access on a
nullvalue returnsnullwithout any warning whatsoever.
Accessing the property of anullvalue gives you a notice, while calling a function on thatnullvalue gives you a fatal error.This is pretty inconsistent. IMO all of these should be fatal errors, although I realize that might be a bit drastic for PHP 8.
We should at least give the user a notice when using array access on anullvalue.Any thoughts?
wouldn't that break code like below which is IMHO the best usecase of
the PHP 5.6 feature that you can directly access a array field of a
function result and the type-casting encsues that the return-type is
always intfunction GetSKatMaxSort(int $archive=0): int
{
return (int)mysqli_fetch_row($this->db->query("select SQL_NO_CACHE
max(ssort) from {$this->db->table} where sarchive=$archive", 1))[0];
}
Am 20.09.2017 um 19:23 schrieb ilija.tovilo@me.com:
I realized after sending the initial E-Mail that there might be more
breaking changes than I thought, like the one you suggested.The same thing actually happens for bools, ints and floats:
https://3v4l.org/jvVbOI wouldn’t depend on this behaviour since you’d get errors depending on
what is returned, like an empty array, a string, an object or resource.
i would depend on that behavior because in reality you can get back two
different things:
- a numeric array with exactly index 0
-
NULL
how do you imagine mysqli_fetch_row() return empty array, string or even
object/resource? when nothing is found it's a non-empty array and
otherwise NULL
before PHP 5.6 (or PHP 5.5 not sure) these where useless multiple lines
because you needed to store the result of mysqli_fetch_row() in some var
to access key 0
Am 20.09.2017 um 19:06 schrieb ilija.tovilo@me.com:
Hi everybody!
To my surprise, I noticed today that an array access on a
null
value returnsnullwithout any warning whatsoever.
Accessing the property of anullvalue gives you a notice, while
calling a function on thatnullvalue gives you a fatal error.This is pretty inconsistent. IMO all of these should be fatal errors,
although I realize that might be a bit drastic for PHP 8.
We should at least give the user a notice when using array access on
anullvalue.Any thoughts?
wouldn't that break code like below which is IMHO the best usecase of
the PHP 5.6 feature that you can directly access a array field of a
function result and the type-casting encsues that the return-type is
always intfunction GetSKatMaxSort(int $archive=0): int
{
return (int)mysqli_fetch_row($this->db->query("select SQL_NO_CACHE
max(ssort) from {$this->db->table} where sarchive=$archive", 1))[0];
}
To my surprise, I noticed today that an array access on a
nullvalue returnsnullwithout any warning whatsoever.
Accessing the property of anullvalue gives you a notice, while calling a function on thatnullvalue gives you a fatal error.This is pretty inconsistent. IMO all of these should be fatal errors, although I realize that might be a bit drastic for PHP 8.
We should at least give the user a notice when using array access on anullvalue.
There is already a respective RFC[1] which has been accepted for PHP
7.2, but there are issues[2].
[1] https://wiki.php.net/rfc/notice-for-non-valid-array-container
[2] http://news.php.net/php.internals/97520
--
Christoph M. Becker