Error in PHP 7.4: ErrorException array_key_exists() expects parameter 2 to be array, int give given
1. Error in PHP 7.4: ErrorException array_key_exists() expects parameter 2 to be array, int give. as shown in Figure 1
2. The code is implemented as follows, the reason is that require($this->getCachePath()) returns 1, and the root of the return 1 is the path $this->getCachePath() The content in the represented file is empty.
if (array_key_exists($path, $this->assetEntries)) {
return $this->assetEntries[$path];
}
if (!$this->isThemeUpdated()) {
$this->assetEntries = require($this->getCachePath());
return;
}
$code = '<?php';
$code .= "\n\n";
$code .= 'return ' . var_export($assets, true) . ';';
$this->local->put($this->getCachePath(), $code, true);
3. In fact, when using require, it is a statement, not a function, so require($this->getCachePath()) can be replaced with require $this->getCachePath()
4. Reference:Include , the value of $BAR is 1 because the include run successfully. Notice the difference in the above example. The first one uses return in the included file and the other does not. If the file cannot be included, it returns FALSE and an e_warning warning is issued. as shown in Figure 2
5. Then it is speculated that the reason is that although the content in the included file is expected to be return array() , in fact, the content of the file is not the same. Because the contents of the file are written based on $this->local->put($this->getCachePath(), $code, true);
6. Simulate the actual operation of the code, require will return the case of array().
return.php
<?php
return [
'a' => 1,
'b' => 2
];
?>
require.php
<?php
var_dump(require('return.php'));
?>
array(2) {
["a"]=>
int(1)
["b"]=>
int(2)
}
7. Simulate the actual operation of the code, require will return the case of 1 (1), and return the number 1 in the included file.
return.php
<?php
return 1;
?>
require.php
<?php
var_dump(require('return.php'));
?>
int(1)
8. Simulate the actual operation of the code, require will return 1 (2), including the content in the php tag in the file is empty.
return.php
<?php
?>
require.php
<?php
var_dump(require('return.php'));
?>
int(1)
9. Simulate the actual operation of the code, require will return 1 (3), the content in the containing file is empty, and it is a blank file.
return.php
require.php
<?php
var_dump(require('return.php'));
?>
int(1)
10. In the production environment, require($this->getCachePath()) returns 1, then require will return 1 In the case (3), the content in the containing file is empty, and it is a blank file.
11. Check the put() method in illuMinate\FileSystem\FileSystem to implement
public function put($path, $contents, $lock = false)
{
return file_put_contents($path, $contents, $lock ? LOCK_EX : 0);
}
12. For specific investigation and analysis process, please refer to:File_put_contents() with empty file contents when using file_put_contents() in PHP 7.4.

