Viewed   62 times

I want to write a function that does some dirty work logging a transaction, but the anonymous function scope does not seem to register the parent scope $db and $value variables. How can I pass the variables into the closure?

Ironically, the SO tag 'closures' does not describe the PHP version of it very accurately...?

class controller
{
    function submit()
    {
        $db = new database();
        $result = $db->execute_tx(function() {
            $db->insert_model_a($value_a); // ERROR: $db is non-object
            $db->insert_model_b($value_b);
        });
    }
}

class database
{
   function execute_tx($atomic_action)
   {
        try
        { 
            $this->start();
            $atomic_action();
            $this->commit();
            // etc..
        }
        catch(...)
        { 
            $this->rollback();
            // etc..
        } 
        finally
        {
            // etc..
        }
   }

   function insert_model_a() { ... }
   function insert_model_b() { ... }
}

 Answers

1

Use the use keyword to bind variables into the function's scope.

function() use ($db) {

Closures may also inherit variables from the parent scope. Any such variables must be declared in the function header [using use].

http://www.php.net/manual/en/functions.anonymous.php

Sunday, November 20, 2022
2

$apples will take on the value that is passed to the function when it is called, e.g.

function my_method($callback) {
    // inside the callback, $apples will have the value "foo"
    $callback('foo'); 
}

$oranges will refer to the value of the variable $oranges which exists in the scope where you defined the closure. E.g.:

$oranges = 'bar';

my_method(function($apples) use ($oranges) {
    // $oranges will be "bar"
    // $apples will be "foo" (assuming the previous example)
});

The differences is that $oranges is bound when the function is defined and $apples is bound when the function is called.


Closures let you access variables defined outside of the function, but you have to explicitly tell PHP which variables should be accessible. This is similar (but not equivalent!) to using the global keyword if the variable is defined in global scope:

$oranges = 'bar';

my_method(function($apples) {
    global $oranges;
    // $oranges will be "bar"
    // $apples will be "foo" (assuming the previous example)
});

The differences between using closures and global:

  • You can bind local variables to closures, global only works with global variables.
  • Closures bind the value of the variable at the time the closure was defined. Changes to the variables after the function was defined does not effect it.
    On the other hand, if you use global, you will receive the value the variable has at the moment when the function is called.

    Example:

    $foo = 'bar';
    $closure = function() use ($foo) { 
        echo $foo; 
    };
    $global = function() {
        global $foo;
        echo $foo;
    };
    
    $foo = 42;
    $closure(); // echos "bar"
    $global(); // echos 42
    
Saturday, September 10, 2022
3

variables inherited from the outer scope need to be listed explicitely. from the manual:

public function getTotal($tax)
{
    $total = 0.00;

    $callback =
        function ($quantity, $product) use ($tax, &$total)
...
Saturday, November 19, 2022
2

Cracks knuckles

Technically the syntax is "correct" (it won't generate a fatal error) but the semantics of PHP render it effectively meaningless in its current form. Let's look at a few things first, namely how PHP handles the assignment of named functions to variables:

php > echo shell_exec("php -v");
PHP 5.4.16 (cli) (built: Oct 30 2018 19:30:51)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

php > function speak($arg) {echo "{$arg}n";}
php > function give($arg) {return $arg;}
php > $speak = speak(4);
4
php > $give = give(4);
php > var_dump($speak);
NULL
php > var_dump($give);
int(4)

The function itself is executed upon assignment and its return value (NULL or otherwise) is assigned to the variable. Since we're only assigning the return value of a function's execution, trying to use this variable as a function name has no use:

php > $speak(4);
php > $give(4);
php >

Let's contrast this to the assignment of an anonymous function (a.k.a. a 'Closure'):

php > $min = 1; $max = 6;
php > $checkName = function ($value) use ($min, $max) {
php {   echo "value: {$value}n";
php {   echo "min: {$min}n";
php {   echo "max: {$max}n";
php { };
php > var_dump($checkName);
object(Closure)#1 (2) {
  ["static"]=>
  array(2) {
    ["min"]=>
    int(1)
    ["max"]=>
    int(6)
  }
  ["parameter"]=>
  array(1) {
    ["$value"]=>
    string(10) "<required>"
  }
}

Unlike some other languages, a closure is represented in PHP by an actual Object. Variables inside the 'use' clause are imported at the time the Closure was created; function parameters (i.e. $value) have their values captured when the Closure is called (hence why we see it noted as a required parameter and not a static value). The semantics of references within Closures aren't worth considering right now but if you want further reading, goat's answer to this question is a great start.

The major takeaway here is that the Closure's assignment to $checkName did not execute the Closure itself. Instead, $checkName becomes a sort of "alias" we can use to reference this function by name:

php > $checkName("hello ");
value: hello 
min: 1
max: 6
php >

Given how loose PHP is about the number of function parameters passed, a zero-parameter execution returns expected results:

php > $checkName();
value:
min: 1
max: 6
php >

Now let's take it another level deeper and define a function within a function:

php > function myOuterFunc($arg) {
php {   function myInnerFunc($arg){
php {     echo "{$arg}n";
php {   }
php { }
php > $myVal = myOuterFunc("Hello ");
php > var_dump($myVal);
NULL
php >

By now this result should make sense. Functions do not execute unless explicitly called; just because we call myOuterFunc doesn't mean we execute any function code defined inside of it. That's not to say that we couldn't:

php > function myOuterFunc($arg) {
php {   function myInnerFunc($arg){
php {     echo "{$arg}n";
php {   }
php {   myInnerFunc($arg);
php { }
php > $myVal = myOuterFunc("Hello ");
Hello 
php > var_dump($myVal);
NULL
php >

Which brings us back around to what is essentially your question: what about a named function inside of a Closure? Given what we've now discovered about function execution, we can generate a series of very predictable examples:

$min = 1; $max = 6;
$checkName = function ($value) use ($min, $max) {
  function question(){echo "How are youn";}
  echo "value: {$value}n";
  echo "min: {$min}n";
  echo "max: {$max}n";
};
php > $checkName("Hello ");
value: Hello 
min: 1
max: 6
php >

As expected, the named function's code inside the Closure is not executed because we have not explicitly called it.

php > $min = 1; $max = 6;
php > $checkName = function ($value) use ($min, $max) {
php {   function question(){echo "How are youn";}
php {   echo "value: {$value}n";
php {   echo "min: {$min}n";
php {   echo "max: {$max}n";
php {   question();
php { };
php > $checkName("Hello ");
value: Hello 
min: 1
max: 6
How are you
php >

Explicitly calling the inner function works just fine, provided we define that function before we call it:

php > $min = 1; $max = 6;
php > $checkName = function ($value) use ($min, $max) {
php {   echo "value: {$value}n";
php {   echo "min: {$min}n";
php {   echo "max: {$max}n";
php {   question();
php {   function question(){echo "How are youn";}
php { };
php > $checkName("Hello ");
value: Hello 
min: 1
max: 6
php >

php > $min = 1; $max = 6;
php > $checkName = function ($value) use ($min, $max) {
php {   echo "value: {$value}n";
php {   echo "min: {$min}n";
php {   echo "max: {$max}n";
php {   function question(){echo "How are youn";}
php {   question();
php { };
php > $checkName("Hello ");
value: Hello 
min: 1
max: 6
How are you
php >

So to the point of your questions then:

  1. Yes it's legal and what you're attempting is possible but semantically meaningless in its current form.

  2. Any named functions inside the Closure definitely do not reside in the global namespace, they are within the scope of their defining Closure. FWIW, the term "members" typically refers to class variables (usually called "properties" in PHP). While Closures are an Object and let you duplicate the functionality of instance variables found within classes, they should not be confused or construed with classes in any way.

3/4) Not how you're trying to use it, no. Nothing outside of the Closure has any concept of the functions inside; if in calling your Closure code, said code performs operations using the inner functions, then they will see the light of day. But there is no immediate way to reference those inner functions as if they were defined outside of the Closure's scope.

In other words, the only way you'll get those inner functions to execute is if a) that code is specifically executed by the Closure's code and b) you execute said Closure code.

Hope this helps.

Tuesday, December 20, 2022
 
2
$tweet = "this has a #hashtag a  #badhash-tag and a #goodhash_tag";

preg_match_all("/(#w+)/", $tweet, $matches);

var_dump( $matches );

*Dashes are illegal chars for hashtags, underscores are allowed.

Tuesday, October 4, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :