Bug #80228 [Opn->Nab]: Strct types fatal error
| From: | cmb@php.net | Date: | Tue, 13 Oct 2020 08:40:41 +0000 |
| Subject: | Bug #80228 [Opn->Nab]: Strct types fatal error | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-229591@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80228&edit=1
ID: 80228
Updated by: cmb@php.net
Reported by: daler445 at gmail dot com
Summary: Strct types fatal error
-Status: Open
+Status: Not a bug
Type: Bug
Package: Variables related
Operating System: All
PHP Version: 7.4.11
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
This is documented[1] behavior:
| Additionally the following key casts will occur:
|
| Strings containing valid decimal integers, unless the number is
| preceded by a + sign, will be cast to the integer type.
[1] <https://www.php.net/manual/en/language.types.array.php>
Previous Comments:
------------------------------------------------------------------------
[2020-10-13 08:32:51] daler445 at gmail dot com
Description:
------------
Strict types seem to throw compile-time fatal error on retrieving data, if you pass integer in
quotes. It defines string as integer.
I guess, it's working like this:
<?php
$string = '12345';
$arr = [$string => 1];
var_dump(array_keys($arr));
which returns:
array(1) {
[0] => int(12345)
}
Bug was discovered after clear php install, no ini files was edited.
Test script:
---------------
<?php
declare(strict_types=1);
class Test {
private $storage = [];
public function setData(string $key, int $value): void
{
$this->storage[$key] = $value;
}
public function getAllData(): array
{
return array_keys($this->storage);
}
}
$test = new Test;
$test->setData('key', 1);
$test->setData('123', 2);
foreach ($test->getAllData() as $data) {
echo gettype($data) . "\n";
}
Expected result:
----------------
string string
Actual result:
--------------
string integer
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80228&edit=1