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


1 comment:

  1. Hello, this post gives very interesting information about off-season camping, really I like this information which is so much beneficial to us, keep sharing such kind of information, Thanks for sharing.
    PhpStorm Crack

    ReplyDelete

Débutez en crypto-monnaie de manière ludique avec les projets "Move to earn"

Les projets "Move to earn" sont des projets très en vogue en ce moment dans le monde de la crypto-monnaie. Le principe est simple ...