0

I'm currently implementing a PHP anonymus function.

class bar {
  public function foo()
  {
    $start = 1;

    add_one(function() {
      $start++;
    })
  }
} 

function add_one($func) {
  $func();  
}

This return an error because $start is actually defined outside from the anonymus function, its not a local variable in the anonymus function. anybody can solve this ?, how to pass the value ?

Thanks.

Marcel Ang
  • 119
  • 1
  • 11
  • `add_one(function() use ($start) { $start++; })` though your function doesn't really do very much – Mark Baker May 30 '16 at 13:39
  • 1
    @MarkBaker Hey Mark. I think variables used via `use` are not passed by reference `use (&$start)` – Daniel W. May 30 '16 at 13:48
  • Ah, thanks guys, sorry for the duplicate question. I cannot find those question before. @Mark : yeah, that function doesn't really do much, its just a simple example i made imitating the real case. But thanks anyway. – Marcel Ang May 31 '16 at 01:17

0 Answers0