1

I'm working in the latest version of PHP, within a class, and writing a sorting function which includes the following:

public static function sort_alphabetically($data, $sortBy=null)
{
    ...

    if($sortBy)
    {
        function sortBy($a, $b) use ($sortBy)
        {
            if ($a->$sortBy == $b->$sortBy){return 0;}
            if ($a->$sortBy > $b->$sortBy){return 1;}
            else{return -1;}
        };

        usort($data, "sortBy");
    }
    return $data;
}

yet I'm consistently getting

Parse error: syntax error, unexpected 'use' (T_USE), expecting '{'...

It's difficult to google the word use and get useful results, but I have seen a mention that it can't be used within a class or namespace. However, the PHP documentation does exactly that - http://php.net/manual/en/functions.anonymous.php - so I am not sure whether the author of that bit of the Internet was correct.

Can anyone shed some light on this? Is there an alternative method I could use?

5
  • 6
    If a function has a name, it's not anonymous Commented Oct 20, 2014 at 14:45
  • true. I had overlooked that, thanks Commented Oct 20, 2014 at 15:03
  • .. and it's a parse error, not an exception. Commented Oct 20, 2014 at 15:03
  • yeah yeah...technicalities... ;) Commented Oct 20, 2014 at 15:04
  • Possible duplicate of Use php namespace inside function Commented Sep 18, 2018 at 20:21

4 Answers 4

3

The use keyword is (not the only use case) used to import variables into closures or anonymous functions (other uses are the import of namespaces or traits).

That does not apply to conventional functions that have a name (like in your case). What you did is just nesting a function into another function. In order to be able to import variables into a closure or an anonymous function you need to declare one first.

You have the following options:

public static function sort_alphabetically($data, $sortBy=null)
{
    ...

    if($sortBy)
    {
        usort($data, function($a, $b) use ($sortBy){
            if ($a->$sortBy == $b->$sortBy){return 0;}
            if ($a->$sortBy > $b->$sortBy){return 1;}
            else{return -1;}        
        });
    }
    return $data;
}

Here you would make use of a closure and import the $sortBy variable into it.

public static function sort_alphabetically($data, $sortBy=null)
{
    ...

    if($sortBy)
    {
        $callback = function($a, $b) use ($sortBy){
            if ($a->$sortBy == $b->$sortBy){return 0;}
            if ($a->$sortBy > $b->$sortBy){return 1;}
            else{return -1;}        
        };

        usort($data, $callback);
    }
    return $data;
}

In this case an anonymous function is being used.

Sign up to request clarification or add additional context in comments.

Comments

0

The use language construct is valid for anonymous functions only. Your function is not anonymous because you gave it a name.

public static function sort_alphabetically($data, $sortBy=null)
{
    ...

    if($sortBy)
    {
        $f = function($a, $b) use ($sortBy)
        {
            if ($a->$sortBy == $b->$sortBy){return 0;}
            if ($a->$sortBy > $b->$sortBy){return 1;}
            else{return -1;}
        };

        usort($data, $f);
    }
    return $data;
}

Remove the name, you should be fine.

2 Comments

This won't work because the call to usort in your example has no access to the now unammed function. sortBy is referenced by name as the second parameter
Woops, forgot about that.. fixed
0

You can't use use unless it's an anonymous function or closure. Assuming you're not reusing the function elsewhere you can easily adjust your code to suit, as below. Similarly you could assign the function to a variable and use the variable as the second argument of usort

public static function sort_alphabetically($data, $sortBy=null)
{
    if($sortBy)
    {
        usort($data, function ($a, $b) use ($sortBy)
        {
            if ($a->{$sortBy} == $b->{$sortBy}){return 0;}
            if ($a->{$sortBy} > $b->{$sortBy}){return 1;}
            else{return -1;}
        });
    }

    return $data;
}

Comments

-1

move the function outside of the other function. nesting functions is really hard to read, especially the way you are using the names within this function. it's most likely causing the compiler issue.

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.