没有不值得去解决的问题,也没有不值得去学习的技术!

Error in PHP 7.4: ErrorException array_key_exists() expects parameter 2 to be array, int given

The value of $bar is 1 because include executed successfully. Note the difference in the examples above. The first one uses return in the included file, while the other does not. If the file cannot be included, it returns false and issues an E_WARNING warning

1. Error in PHP 7.4: ErrorException array_key_exists() expects parameter 2 to be array, int given. See Figure 1.

Error in PHP 7.4: ErrorException array_key_exists() expects parameter 2 to be array, int given
Figure 1

2. The code implementation is as follows. The cause is that require($this->getCachePath()) returns 1, and the root cause of returning 1 is that the content of the file at the path $this->getCachePath() is empty.

PHP
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. Therefore, require($this->getCachePath()) can be replaced with require $this->getCachePath()

4. Reference: include. The value of $bar is 1 because include executed successfully. Note the difference in the examples above. The first one uses return in the included file, while the other does not. If the file cannot be included, it returns false and issues an E_WARNING warning. See Figure 2.

The value of $bar is 1 because include executed successfully. Note the difference in the examples above. The first one uses return in the included file, while the other does not. If the file cannot be included, it returns false and issues an E_WARNING warning
Figure 2

5. I infer that the reason is that although the content of the included file is expected to be return Array(), it is actually not. The file content is written based on $this->local->put($this->getCachePath(), $code, true);.

6. Let’s simulate the actual execution of the code where require returns Array().

return.php

PHP
<?php
 
return [
    'a' => 1,
    'b' => 2
];
 
?>

require.php

PHP

<?php
 
var_dump(require('return.php'));
 
?>
Plaintext

array(2) {
  ["a"]=>
  int(1)
  ["b"]=>
  int(2)
}


7. Let’s simulate the actual execution of the code where require returns 1 (scenario 1): the included file returns the integer 1.

return.php

PHP
<?php
 
return 1;
 
?>


require.php

PHP
<?php
 
var_dump(require('return.php'));
 
?>
Plaintext


int(1)



8. Let’s simulate the actual execution of the code where require returns 1 (scenario 2): the content inside the PHP tags in the included file is empty.

return.php

PHP
<?php
 
?>

require.php

PHP
<?php
 
var_dump(require('return.php'));
 
?>
Plaintext


int(1)



9. Let’s simulate the actual execution of the code where require returns 1 (scenario 3): the content of the included file is empty, making it a blank file.

return.php

PHP
/* 此处为空 */

require.php

PHP
<?php
 
var_dump(require('return.php'));
 
?>
Plaintext


int(1)



10. In the production environment, the case where require($this->getCachePath()) returns 1 falls under scenario 3, where the content of the included file is empty, making it a blank file.

11. View the implementation of the put() method in Illuminate\Filesystem\Filesystem.

PHP
    public function put($path, $contents, $lock = false)
    {
        return file_put_contents($path, $contents, $lock ? LOCK_EX : 0);
    }

12. For the specific troubleshooting and analysis process, refer to: Troubleshooting empty file contents when using file_put_contents() in PHP 7.4.

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系