Pages

Sunday, July 11, 2021

PHPStorm Warning : Illegal string offset

 

Here is a comment about "Illegal string offset warning" I had in phpstorm.



I found the solution on StackOverflow : 

The error Illegal string offset 'whatever' in... generally means: you're trying to use a string as a full array.


In my case, I defined the following :

    /**
     * isAuthorized
     * @param string $user user authorized
     */
    public function isAuthorized($user)
    {
        // Autorisations
        // admin peut tout faire
        if ($user['role'] === 'admin') {
            return true;
        }

In fact, I made a mistake. The $user variable was defined in method comment as a string. But, $user is an array ! That's why I had this warning.

To avoid this warning, you have to change the type of the variable in method comment and a good practice is to add an "s" at the end of the variable name in order to identify easily the array variable. 


    /**
     * isAuthorized
     * @param array $users user authorized
     */
    public function isAuthorized($users)
    {
        // Autorisations
        // admin peut tout faire
        if ($users['role'] === 'admin') {
            return true;
        }


No comments:

Post a Comment

Publication android : échec de la demande d'accès en production

  Après avoir attendu 14 jours avec 12 testeurs, j'ai pu enfin  faire une demande d'accès en production .C'est long et on se dit...