Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal string offset with php 7.1.6

Tags:

php

This code throws a waring under PHP 7.1.6... Under PHP 5.x.x it does not have any problems.

The offending line is $attributes['onclick'] = $onclick;, with the warning Illegal string offset 'onclick'.

Here is my code:

protected function js_anchor($title, $onclick = '', $attributes = '')
    {
        if ($onclick)
        {
            $attributes['onclick'] = $onclick;
        }

        if ($attributes)
        {
            $attributes = _parse_attributes($attributes);
        }

        return '<a href="javascript:void(0);"'.$attributes.'>'.$title.'</a>';
    }
like image 549
Kokako Avatar asked Jul 20 '17 02:07

Kokako


1 Answers

$attributes is initialized as an empty string. You need to make it an empty array, $attributes = []

protected function js_anchor($title, $onclick = '', $attributes = [])
{
    if ($onclick)
    {
        $attributes['onclick'] = $onclick;
    }

    if ($attributes)
    {
        $attributes = _parse_attributes($attributes);
    }

    return '<a href="javascript:void(0);"'.$attributes.'>'.$title.'</a>';
}
like image 152
Kai Avatar answered Oct 10 '22 12:10

Kai