Unexpected '[', how to resolve?

Asked

Viewed 40 times

0

I have this mistake on line 38, the code is like this

syntax error, unexpected '[', expecting ')'vendor/sendgrid/sendgrid/lib/helpers/mail/Mail.php on line 38

The code:

public function jsonSerialize()
    {
        return array_filter(
            [
                'email' -> $this->getEmail()
            ]

        );
    }

I think it might be incompatibility with server, because on my localhost it works, the server PHP and 5.4, someone knows how to solve

  • 2

    The notation [] for arrays was just added in version 5.4, so it shouldn’t be a problem; allied to this, there is no such thing in PHP ->. To arrays associative uses =>.

  • Errata: "Does not exist in PHP" when the context is array. Exists when treated of objects.

  • That said and considering the fact that the error occurred in a third party library file, sendgrid/sendgrid, I’d say it’s not enough that you fix the syntax in the file. If there is an error due to syntax incompatibility it is because you are using libraries incompatible with the installed version (or vice versa). The ideal is to upgrade PHP to a version that satisfies all its dependencies.

1 answer

1

If the PHP version is less than 5.4, the short array statement will not work, as this feature was added in version 5.4.0. In addition, assigning a value to an array key must be done using the symbol =>. The symbol -> is used to access properties and/or methods of an object.

public function jsonSerialize()
{
    return array_filter(array(
                'email' => $this->getEmail() //Arrume o símbolo
    ), 'callback');
}

To trigger consciousness, use the full form to declare the array and pass its callback function in the second parameter of the array_filter. If it works, probably your server’s PHP is under 5.4.

  • It is worth mentioning that as the error gave in an archive of a third party library, sendgrid/sendgrid, editing it would not be the first solution. If there was a syntax error here, it will probably be in the entire library and correcting it entirely would probably be impractical.

  • Then I can fix the error, so probably my Rv is less than 5.4, I would have some other solution, besides updating it Rv, because it and the old system in it ta everything with lower php language we are working on a new but I can not use it for now

  • The ideal would be to use libraries that are compatible with the PHP version of your server.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.