php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80228 Strct types fatal error
Submitted: 2020-10-13 08:32 UTC Modified: 2020-10-13 08:40 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: daler445 at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Variables related
PHP Version: 7.4.11 OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: daler445 at gmail dot com
New email:
PHP Version: OS:

 

 [2020-10-13 08:32 UTC] 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 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-10-13 08:40 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-10-13 08:40 UTC] cmb@php.net
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>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 23:01:29 2024 UTC