I recently upgraded Ubuntu from 20.04 LTS to 22.04 LTS. I noticed that PHP was upgraded to the following version:
PHP 8.1.2-1ubuntu2.13 (cli) (built: Jun 28 2023 14:01:49) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2-1ubuntu2.13, Copyright (c), by Zend Technologies
My PHP application stopped working in this part:
public function __set($name, $value)
{
$this->$name = $value;
}
public function DisplayMenu($buttons)
{
echo "\t\t\t<nav>\n\t\t\t\t<ul>\n";
while (list($name, $url) = each($buttons)) {
$this->DisplayButton($name, $url);
}
echo "\t\t\t\t</ul>\n\t\t\t</nav>\n";
}
The accessor magic method stopped working after the upgrade from PHP 7.x to 8.x. What is the new way to use the equivalent in PHP 8.x? Thank you.
It's not the accessor. The problem is that the each
function was removed in PHP 8. You'll need to switch to a foreach
loop.
<?php
class what
{
public function __set($name, $value)
{
$this->$name = $value;
}
public function DisplayButton($name,$value)
{
$this->$name = $value;
}
public function DisplayMenu($buttons)
{
echo "\t\t\t<nav>\n\t\t\t\t<ul>\n";
foreach( $buttons as $name => $url ) {
$this->DisplayButton($name, $url);
}
echo "\t\t\t\t</ul>\n\t\t\t</nav>\n";
}
}
$x = new what();
$x->DisplayMenu(['a'=>'b','c'=>'d']);
print_r($x);
?>