Kokako
Kokako

Reputation: 85

Illegal string offset with php 7.1.6

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>';
    }

Upvotes: 5

Views: 12582

Answers (1)

Kai
Kai

Reputation: 2599

$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>';
}

Upvotes: 9

Related Questions