Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass argument in callback function in php?

Tags:

php

I have Repository class with a method as follows:

public function GetOne($id){
    $method = __METHOD__;
    $post = null;


    $post = $this->CacheManager($method, function($id) {
        return DB::select("select * from posts where id = ?", [$id]);
    });

    return $post;
}

I want to cache the result, but in the closure/callback function the $id parameter is not working. The CacheManager is a trait where I'm using it in my repository.

public function CacheManager($method, $fn) {
  $obj = null;

  if(!$this->HasCache($method)){
    $obj = $fn();
  }else {
    $obj = $this->GetCache($method);
  }

  return $obj;
}

I have some other methods without parameters and they're working as intended.

like image 788
marko Avatar asked Jan 27 '16 10:01

marko


People also ask

What is callback function in PHP with example?

A callback function (often referred to as just "callback") is a function which is passed as an argument into another function. Any existing function can be used as a callback function.

What is the callback argument?

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

Can you pass functions as parameters in php?

There is another type of function known as PHP Parameterized functions, these are the functions with pre defined parameters. You'll pass any number of parameters inside a function. These passed parameters act as variables in your function. They are declared inside the brackets, after the function name.

What is callable type in PHP?

Definition and Usage The callable keyword is used to force a function argument to be a reference to a function. A callable can be one of the following: An anonymous function. A string containing the name of a function. An array describing a static class method.


1 Answers

Use use. :D

With the use clause, you can import variables from the parent scope into the scope of the function.

public function GetOne($id){
    $method = __METHOD__;
    $post = null;


    $post = $this->CacheManager($method, function() use ($id) {
        return DB::select("select * from posts where id = ?", [$id]);
    });

    return $post;
}

Just a side note. Since it looks you are building a caching mechanism, you will need to include the ID in the cache as well. Currently you only check by $method, but for each id you will probably have a different cache entry which may or may not exist. So I think in your function you need to do something like the line below to make the cache key more unique. I would also call the parameter $method something like $cacheKey instead, since to the cache it shouldn't be linked to a method name per se.

$method = __METHOD__ . ";$id";

Update for PHP 7.4: arrow functions

The RFC for arrow functions (AKA 'short closures') has passed voting.

With these you don't specify the parameters you want to close in, because they can only have a single expression anyway, so any expression/value they use can (and will) be taken from the parent function scope.

Since in this case the anonymous function just has a single statement, it can be rewritten into an arrow function. The call to the cache manager will then look like this:

public function GetOne($id){
    $method = __METHOD__;
    $post = null;

    $post = $this->CacheManager($method, fn() => DB::select("select * from posts where id = ?", [$id]));

    return $post;
}
like image 109
GolezTrol Avatar answered Oct 09 '22 06:10

GolezTrol