Julien
Julien

Reputation: 1980

Cannot use correctly string offset as an array

I am using an old PHP hook class and have a problem with my PHP update
Here is the mistake I have

[php7:error] PHP Fatal error:  Uncaught Error: Cannot use string offset as an array in /var/www/html/libs/Hooks.class.php:202\nStack  

Here is my source code (ligne 202)

function add_hook($tag, $function, $priority = 10) {
    if (! isset ( $this->hooks [$tag] )) {
        die ( "There is no such place ($tag) for hooks." );
    } else {
        $this->hooks [$tag] [$priority] [] = $function; // <-- this is problem (line 202)
    }
}

If I change the line that causes problem like this:

$this->hooks [$tag] [$priority] = $function; // <-- this is problem (line 202)

I only get the first letter of the content (as in the pictures)

enter image description here

I do not know how to solve the problem

Upvotes: 0

Views: 160

Answers (1)

Vasyl Zhuryk
Vasyl Zhuryk

Reputation: 1248

If you defined var as a string - you cant add array elements. Your solution - to cnange library function to:

function set_hook($tag) {
    $this->hooks [$tag] = [];
}

and for better quality of the code - change function declaration to:

function add_hook($tag, $function, $priority = "10") {

Upvotes: 1

Related Questions