Error in PHP 7.4: Cannot RedeClare getFirstSkuIndex(), set as a static anonymous function, and assign a value to the variable
1. Report an error in PHP 7.4: Cannot RedeClare GetFirstSkuIndex(). Indicates that the function getFirstSkuIndex() has been defined. PHP does not support redefining declared functions. as shown in Figure 1
[2023-01-09 10:28:00] local.ERROR: Cannot redeclare getFirstSkuIndex() (previously declared in E:\wwwroot\object\storage\framework\views\f7c14c1d27c4ea3f4d0e1c60a5009bfe9a47c438.php:7) {
"exception": "[object] (Symfony\\Component\\Debug\\Exception\\FatalErrorException(code: 64): Cannot redeclare getFirstSkuIndex() (previously declared in E:\\wwwroot\\object\\storage\\framework\\views\\f7c14c1d27c4ea3f4d0e1c60a5009bfe9a47c438.php:7) at E:\\wwwroot\\object\\storage\\framework\\views\\f7c14c1d27c4ea3f4d0e1c60a5009bfe9a47c438.php:7)
[stacktrace]
#0 {main}
"
}
2. Reference:https://www.shuijingwanwq.com/2022/11/14/7149/, now because the same file is repeatedly introduced (re-introduced on business). This leads to the same function being duplicated.
3. View the code implementation of the imported file
function getFirstSkuIndex ($product) {
// ...
}
return getFirstSkuIndex($product);
4. Declare the function as a closure function, and then assign a value to a variable, PHP will automatically convert this expression into a built-in class closure object instance. and set it to a static anonymous function. The coded implementation of the edited file, when re-executing the imported duplicate file, the function $GetFirstSkuIndex simply overrides the previous statement. No more errors.
$getFirstSkuIndex = static function ($product) {
// ...
};
return $getFirstSkuIndex($product);
