0

Hello I have the following method. I would like to use a nameless function and change some data before the method returns, instead of creating a separate function to localize the results from the database query. I would also like the method to return the filtered data from the nameless function. What am I doing wrong in the following code?

public function getStats($request){

    // some custom input filtering

    $params = array('uid' => $this->uid);
    $reply = $db->get($query,$params);

    return function() use (&$reply){

        //localization of some strings

        return $reply;
    };
} 

2 Answers 2

1

Instead of returning the value returned by your anonymous function, you're returning the function itself. Try this instead:

public function getStats($request){

    // some custom input filtering

    $params = array('uid' => $this->uid);
    $reply = $db->get($query,$params);

    $myfunction = function() use ($reply){

        //localization of some strings

        return $reply;
    };

    return $myfunction();
} 

Also, no need to pass $reply by reference.

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

Comments

0

In PHP nameless functions are known as anonymous functions or closures. Here is an example:

<?php
$greet = function($name)
{
    printf("Hello %s\r\n", $name);
};

$greet('World');
$greet('PHP');
?>

For more information see docs.

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.