so I have these functions
function a(){
int c = 1;
b(function(){echo $c;});
}
function b($code){
$code();
}
but somehow $c becomes undefined in the anonymous function I know it's bacause that the anonymous function is it's own scope, but is there someway to make this work?
Yes: you can use "use" statement.
function a()
{
$c = 1;
b(function() use ($c) {
echo $c;
});
}
function b($code){
$code();
}
http://php.net/manual/en/language.variables.scope.php
When you put $c
inside a function it's considered to be a local scope
variable.