DEV Community

Seong Bae
Seong Bae

Posted on

Unknown named parameter in PHP 8

I am adding hook support to one of my Laravel projects and was using a lot of the code from this wonderful Laravel Hook package at https://github.com/esemve/Hook. However, this package was built for Laravel 5 and PHP 7. I am using Laravel 10 and PHP 8 so there were some issues I had to fix.

One of the issues was "Unknown named parameter" on the following line:

$output = $output . call_user_func_array($function['function'], $params);

Ih PHP 7, the keys in $params were ignored. However, in PHP 8, they are not - keys are converted to named parameters.

So if $params was following:

$params = ['key1'=>'value1', 'key2'=>'value2']

key1 and key2 would be converted to $key1 and $key2 in PHP 8 when they were simply ignored in PHP 7. This was a breaking change in PHP 8.

If you ever come across this issue, a simple fix it to convert $params to array values. Changing to following should fix the issue.

$output = $output . call_user_func_array($function['function'], array_values($params));

Happy coding!

Sources:

Top comments (1)

Collapse
 
lalami profile image
Salah Eddine Lalami

Hi @seongbae , welcome to dev community