2

Parse error: syntax error, unexpected '[' in $results = [];

function class_uses_recursive($class)
{
    $results = [];

    foreach (array_merge([$class => $class], class_parents($class)) as $class)
    {
        $results += trait_uses_recursive($class);
    }

    return array_unique($results);
}

please help me

2

3 Answers 3

19

If you're using an older version of PHP (pre-5.4 I think), this syntax isn't supported:

$results = [];

You'd have to use the older version:

$results = array();
Sign up to request clarification or add additional context in comments.

1 Comment

Did they remove it again in 7.3? I got the same error in PHP 7.3 and was able to fix it by replacing [] with array()
2

You will get this error for any PHP version below 5.4, as the short array syntax [] was not introduced until 5.4. You need to use array() to instantiate arrays in PHP 5.3.x and earlier:

function class_uses_recursive($class)
{
    $results = array();

    foreach (array_merge(array($class => $class), class_parents($class)) as $class)
    {
        $results += trait_uses_recursive($class);
    }

    return array_unique($results);
}

PHP docs on arrays here.

Comments

0

Array is initialised by

$results = array(); // use this


 function class_uses_recursive($class)
    {
        $results = []; //instead of this .

        foreach (array_merge([$class => $class], class_parents($class)) as $class)
        {
            $results += trait_uses_recursive($class);
        }

        return array_unique($results);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.