如何重新编码 PHP 按引用分配的用法

How to re-code PHP assign-by-reference usage

我的弃用检查器抛出此错误:

Using deprecated language feature assign by reference(&=) Since PHP 5.3 use normal assignment instead.

所以,我想弄清楚如何重新编码此 class 中的方法以不使用 by reference 或至少正确使用它(如果完全允许的话 - 这我也不清楚)。

下面是 class 使用 by reference 的部分。整个class是here, the test and the deprecation checker log is here.

我需要一些帮助来重新编码 class 以删除对 by reference

的使用
class ParameterBag
{
    /**
     * Sets value.
     *   can use 'key' = ['subkey' => value, 'subkey2' => value2]
     *      or
     *   'key.subkey' = value
     *   'key.subkey2' = value2
     *
     * @param $key
     * @param $value
     */
    public function set($key, $value)
    {
        $parameters = &$this->resolvePath($key, true);
        $key = $this->resolveKey($key);
        $parameters[$key] = $value;
    }

    /**
     * Resolves a path in parameters property and returns it as a reference.
     *
     * This method allows structured namespacing of parameters.
     *
     * @param string  $key         Key name
     * @param boolean $writeContext Write context, default false
     *
     * @return array
     */
    private function &resolvePath($key, $writeContext = false)
    {
        $array = &$this->parameters;
        $key = (strpos($key, $this->ns) === 0) ? substr($key, 1) : $key;

        // Check if there is anything to do, else return
        if (!$key) {
            return $array;
        }

        $parts = explode($this->ns, $key);
        if (count($parts) < 2) {
            if (!$writeContext) {
                return $array;
            }

            $array[$parts[0]] = [];

            return $array;
        }

        unset($parts[count($parts) - 1]);

        foreach ($parts as $part) {
            if (!array_key_exists($part, $array)) {
                if (!$writeContext) {
                    return $array;
                }

                $array[$part] = [];
            }

            $array = &$array[$part];
        }

        return $array;
    }
}

这似乎是弃用工具中的错误。根据Deprecated features in PHP 5.3.x:

  • Assigning the return value of new by reference is now deprecated.
  • Call-time pass-by-reference is now deprecated.

但是通常不反对通过引用赋值,Returning References 表示:

To use the returned reference, you must use reference assigment