PHP 8.0+ changes behaviour of ReflectionClass::newInstanceArgs, which now assumes that an associative array is an array of named parameters, trying to match array keys to names of arguments in the constructor. This leads to errors like:
zend-form\library\Zend\Form\Element.php:2117
Error: Unknown named parameter $type
To solve the issue for all PHP versions, associative arrays have to be wrapper in array_values(). This does not concern cases where ReflectionClass::newInstanceArgs is used with numerical arrays.
Issue and solution can be tested on all PHP versions with this simple script:
<?php
class Test
{
public $options;
public function __construct(array $options)
{
$this->options = $options;
}
}
$options = array('options' => array('tags' => array(1, 2, 3)));
$reflectionClass = new ReflectionClass('Test');
$instance = $reflectionClass->newInstanceArgs(array_values($options));
var_dump($instance->options);
PHP 8.0+ changes behaviour of ReflectionClass::newInstanceArgs, which now assumes that an associative array is an array of named parameters, trying to match array keys to names of arguments in the constructor. This leads to errors like:
To solve the issue for all PHP versions, associative arrays have to be wrapper in array_values(). This does not concern cases where ReflectionClass::newInstanceArgs is used with numerical arrays.
Issue and solution can be tested on all PHP versions with this simple script: