Welcome! Log In Create A New Profile

Advanced

[PHP-DEV] Raise E_NOTICE for null castings

Posted by Filippo Tessarotto 
Filippo Tessarotto
[PHP-DEV] Raise E_NOTICE for null castings
October 27, 2017 12:00PM
Hi, I would like to propose an RFC to raise an E_NOTICE when a variable
"initialized" to null is casted to other types:

$foo = null;
var_dump($foo['bar']);
var_dump($foo . 'bar');
var_dump($foo + 2);
var_dump($foo & 2);
// At the time being, this code produces no errors

The eventual vote may be split one for each case.

What do you think?

Best regards, Filippo

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
lists@rhsoft.net
Re: [PHP-DEV] Raise E_NOTICE for null castings
October 27, 2017 12:40PM
Am 27.10.2017 um 11:49 schrieb Filippo Tessarotto:
> Hi, I would like to propose an RFC to raise an E_NOTICE when a variable
> "initialized" to null is casted to other types:
>
> $foo = null;
> var_dump($foo['bar']);
> var_dump($foo . 'bar');
> var_dump($foo + 2);
> var_dump($foo & 2);
> // At the time being, this code produces no errors
>
> The eventual vote may be split one for each case.
>
> What do you think?

pleae don't break code like this which is a useful case for access
directly a array field returned from a function and so makes this to a
one-liner without check the result

public function GetMaxSort(): int
{
return (int)mysqli_fetch_row($this->cl_api->db->query("select
SQL_NO_CACHE max(hsort) from {$this->cl_api->sql_prefix}main", 1))[0];
}

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Niklas Keller
Re: [PHP-DEV] Raise E_NOTICE for null castings
October 27, 2017 01:00PM
>
> Hi, I would like to propose an RFC to raise an E_NOTICE when a variable
>> "initialized" to null is casted to other types:
>>
>> $foo = null;
>> var_dump($foo['bar']);
>> var_dump($foo . 'bar');
>> var_dump($foo + 2);
>> var_dump($foo & 2);
>> // At the time being, this code produces no errors
>>
>> The eventual vote may be split one for each case.
>>
>> What do you think?
>>
>
> pleae don't break code like this which is a useful case for access
> directly a array field returned from a function and so makes this to a
> one-liner without check the result
>
> public function GetMaxSort(): int
> {
> return (int)mysqli_fetch_row($this->cl_api->db->query("select
> SQL_NO_CACHE max(hsort) from {$this->cl_api->sql_prefix}main", 1))[0];
>
> }
>

Just use return (int) mysqli_fetch_row($this->cl_api->db->query("select
SQL_NO_CACHE max(hsort) from {$this->cl_api->sql_prefix}main", 1))[0] ?? 0;

Regards, Niklas
lists@rhsoft.net
Re: [PHP-DEV] Raise E_NOTICE for null castings
October 27, 2017 01:30PM
Am 27.10.2017 um 12:54 schrieb Niklas Keller:
> pleae don't break code like this which is a useful case for access
> directly a array field returned from a function and so makes this to
> a one-liner without check the result
>
> public function GetMaxSort(): int
> {
>  return (int)mysqli_fetch_row($this->cl_api->db->query("select
> SQL_NO_CACHE max(hsort) from {$this->cl_api->sql_prefix}main", 1))[0];
>
> }
>
>
> Just use return (int) mysqli_fetch_row($this->cl_api->db->query("select
> SQL_NO_CACHE max(hsort) from {$this->cl_api->sql_prefix}main", 1))[0] ?? 0;

thanks - that's indeed the better solution and since i use
MYSQLI_OPT_INT_AND_FLOAT_NATIVE in the database layer the complete
type-casting can be removed too!

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph M. Becker
[PHP-DEV] Re: Raise E_NOTICE for null castings
October 27, 2017 01:50PM
On 27.10.2017 at 11:49, Filippo Tessarotto wrote:

> Hi, I would like to propose an RFC to raise an E_NOTICE when a variable
> "initialized" to null is casted to other types:
>
> $foo = null;
> var_dump($foo['bar']);
> var_dump($foo . 'bar');
> var_dump($foo + 2);
> var_dump($foo & 2);
> // At the time being, this code produces no errors
>
> The eventual vote may be split one for each case.
>
> What do you think?

Note that there is already an accepted RFC regarding rasing E_WARNING
for invalid container read array-access[1]. However, it has not been
implemented yet, due to difficulties, see the discussion on PR #2031[2].

[1] https://wiki.php.net/rfc/notice-for-non-valid-array-container
[2] https://github.com/php/php-src/pull/2031

--
Christoph M. Becker

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Filippo Tessarotto
[PHP-DEV] Re: Raise E_NOTICE for null castings
October 27, 2017 02:00PM
I am sorry for partial topic duplication regarding array-access.

Still, at least with `declare(strict_types=1);` auto-castings of
non-numbers should raise an error:

var_dump(false + 2);
var_dump(null + 2);

Exactly like `"" + 2`.

WDYT? Filippo

Il 27/10/2017 13:47, Christoph M. Becker ha scritto:
> On 27.10.2017 at 11:49, Filippo Tessarotto wrote:
>
>> Hi, I would like to propose an RFC to raise an E_NOTICE when a variable
>> "initialized" to null is casted to other types:
>>
>> $foo = null;
>> var_dump($foo['bar']);
>> var_dump($foo . 'bar');
>> var_dump($foo + 2);
>> var_dump($foo & 2);
>> // At the time being, this code produces no errors
>>
>> The eventual vote may be split one for each case.
>>
>> What do you think?
>
> Note that there is already an accepted RFC regarding rasing E_WARNING
> for invalid container read array-access[1]. However, it has not been
> implemented yet, due to difficulties, see the discussion on PR #2031[2].
>
> [1] https://wiki.php.net/rfc/notice-for-non-valid-array-container
> [2] https://github.com/php/php-src/pull/2031
>

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login