108

Basically I use this handy function to processing db rows (close an eye on PDO and/or other stuff)

function fetch($query,$func) {
    $query = mysql_query($query);   
    while($r = mysql_fetch_assoc($query)) {
        $func($r);
    }
}

With this function I can simply do:

fetch("SELECT title FROM tbl", function($r){
   //> $r['title'] contains the title
});

Let's say now I need to concatenate all $r['title'] in a var (this is just an example).

How could I do that? I was thinking something like this, but it's not very elegant:

$result = '';
fetch("SELECT title FROM tbl", function($r){
   global $result;
   $result .= $r['title'];
});

echo $result;
1

2 Answers 2

212

You have to use use as described in docs:

Closures may also inherit variables from the parent scope. Any such variables must be declared in the function header. Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing.

Code:

$result = '';
fetch("SELECT title FROM tbl", function($r) use (&$result) {
   $result .= $r['title'];
});

But beware (taken from one of comments in previous link):

use() parameters are early binding - they use the variable's value at the point where the lambda function is declared, rather than the point where the lambda function is called (late binding).

8
  • 1
    Shouldn't that global deceleration be removed ? Dec 6, 2011 at 17:19
  • 20
    +1 for emphasizing the early binding. However I guess in the example above when use (&$result) is passed by reference it doesn't really matter?
    – Dimitry K
    Jun 19, 2014 at 12:13
  • 4
    @DimitryK Yes, reference is used here to bypass default behaviour (early binding). Jun 19, 2014 at 15:42
  • 3
    @machineaddict The basic use is early binding - if you mean workaround for late binding - you would pass the variable through use by reference - using & => use (&$result) and alter $result variable before you call the anonymous function (or something, that calls it)
    – jave.web
    Mar 13, 2016 at 14:14
  • 1
    Since class instances are always passed by reference you wont need to use & for them. (unless you completely overwrite the instance). Jul 24, 2018 at 5:04
0

What about rewriting 'fetch' to call $func only once ?

function fetch($query,$func) {
    $query = mysql_query($query);   
    $retVal = array();
    while($r = mysql_fetch_assoc($query)) {
        $retVal[] = $r;
    }
    $func($retVal);
}

This way you would call $func only once and re-process the array once fetched? Not sure about the performance even tho calling 200 times a function doesn't sound like a good idea.

1
  • Yes you are right. However, you could use mysql_fetch_row() instead of mysql_fetch_assoc() if you're that interested in gaining a few ms here and there... it's just awfully hard to deal with as you'd have to know your columns position. By doing so, you pass from 0.205 to 0.180 on 2000 requests of 30 rows each.
    – user103307
    Dec 6, 2011 at 17:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.