(PHPUnit) PHP Fatal error: Uncaught Error: Call to undefined function each()
Asked Answered
H

6

14

firstly I was receiving a warning and a fatal error. The warning:

Warning:  "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"?

Then I replaced the continue with break and the warning disappeared. But even after the replacement, the fatal error still happens. The fatal error:

PHP Fatal error:  Uncaught Error: Call to undefined function each() in D:\xampp\php\pear\PHPUnit\Util\Getopt.php:80
Stack trace:
#0 D:\xampp\php\pear\PHPUnit\TextUI\Command.php(242): PHPUnit_Util_Getopt::getopt(Array, 'd:c:hv', Array)
#1 D:\xampp\php\pear\PHPUnit\TextUI\Command.php(138): PHPUnit_TextUI_Command->handleArguments(Array)
#2 D:\xampp\php\pear\PHPUnit\TextUI\Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#3 D:\xampp\php\phpunit(46): PHPUnit_TextUI_Command::main()
#4 {main}
  thrown in D:\xampp\php\pear\PHPUnit\Util\Getopt.php on line 80

Fatal error: Uncaught Error: Call to undefined function each() in D:\xampp\php\pear\PHPUnit\Util\Getopt.php:80
Stack trace:
#0 D:\xampp\php\pear\PHPUnit\TextUI\Command.php(242): PHPUnit_Util_Getopt::getopt(Array, 'd:c:hv', Array)
#1 D:\xampp\php\pear\PHPUnit\TextUI\Command.php(138): PHPUnit_TextUI_Command->handleArguments(Array)
#2 D:\xampp\php\pear\PHPUnit\TextUI\Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#3 D:\xampp\php\phpunit(46): PHPUnit_TextUI_Command::main()

The line 77-83 of Getopt.php

reset($args);
array_map('trim', $args);

while (list($i, $arg) = each($args)) {
    if ($arg == '') {
        continue;
}

Im using PHP 8.0.1 and the PHPUnit 9 (at least I think, because I cant use commands to check, and I downloaded it after february 7)

Hemichordate answered 26/2, 2021 at 15:15 Comment(2)
php.net/eachFatima
The class names PHPUnit_Util_Getopt etc. are not from PHPUnit 9. And for years PHPUnit cannot be installed via PEAR.Illimani
M
26

Probably the PHPUnit version you're using is not up-to-date yet for PHP 8 try

while (list($i, $arg) = each($args)) {

into

foreach ($args as $i => $arg) {
Margeret answered 15/1, 2022 at 10:1 Comment(0)
S
17

not an expert of PHPUnit but the "each" function is not available anymore in PHP 8

Warning: This function has been DEPRECATED as of PHP 7.2.0, and REMOVED as of PHP 8.0.0. Relying on this function is highly discouraged.

Taken from the PHP site

Probably the PHPUnit version you're using is not up-to-date yet for PHP 8. Check the version if you can and then see here PHPUnit version support

Simsar answered 26/2, 2021 at 15:23 Comment(0)
P
1

If anyone facing this issue in XAMPP at running Pear commands, then the problem is related to each() function that is deprecated but it is used in many files in the XAMPP. So you have to replace it.

For example: By running only Pear in command line, you will get error on file PEAR.php at line 786:

while (list($k, $objref) = each($_PEAR_destructor_object_list)) {

A straightforward solution is to replace the while and each with the single foreach:

foreach($_PEAR_destructor_object_list as $k => $objref){

It will resolve the issue at this file.

Note: You have to update similarly on all the files of the XAMPP where you face this issue. The command line error will show the file name and line number. You just need to open that file and replace the deprecated code with newer code.

Primordial answered 3/9, 2023 at 8:40 Comment(3)
I would hope there’s an update to PEAR you’d simply need to install which addresses this version compatibility issue…‽Fatima
@Fatima Thank you for the information. As far as I remember, I am using the latest version of xampp but still I face this issue. I fixed the issue by replacing the deprecated code. May be you are also right, there may be a new update or minor update that already fixes these issues. Anyhow, may be it will be helpful for anyone. Thanks.Primordial
@Fatima I just checked, I am using pear version 1.10.1 and the latest version is 1.10.13 So, there is some minor update come. Anyhow I am using xampp latest version and xampp automatically comes with php, pear etc. Thanks again.Primordial
A
0

Test to get your PHPUnit version like this: " vendor\bin\phpunit --version " instead of " phpunit --version "

if it work, you can continue to using your current PHPUnit.

You just need to change " phpunit " by " vendor\bin\phpunit " for each command you want to use.

Ariannaarianne answered 9/8, 2023 at 6:41 Comment(1)
Thanks for contributing, but you aren't answering the actual question. In addition there are already multiple good answers.Periodical
H
0

Try composer update. In my case, just needed to update my PHPUnit files.

Hexapody answered 4/4 at 15:45 Comment(0)
D
-1

Guys i did this coverting to this

foreach ($colonnes as $lib => $pos) {

Disseminule answered 6/3 at 19:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.