zf1s / zf1

Monorepo of a fork of Zend Framework 1, with all components split into individual composer packages. PHP 5.3-8.3 compatible.
BSD 3-Clause "New" or "Revised" License
59 stars 22 forks source link

PHP 8.0+ changes behaviour of ReflectionClass::newInstanceArgs #193

Closed marcing closed 8 months ago

marcing commented 8 months ago

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);