Cant send variable to getFormatter function.
Cant send variable to getFormatter function.
ziv@kpmbro.com
Posts: 73Questions: 28Answers: 4
Hi
I am trying to send some variable to a getFormatter function , but it doesn't work and i cant understand why.
Code example:
$this->editor_instance = new Editor($this->data_table_db_object, $table_params) ;
$test_array=array('a','b','c');
foreach($test_array as $value)
{
$this->editor_instance->fields(Field::inst($value['table_name'].'.'.$value['field'])->getFormatter( function ( $val, $data, $opts ) {
// $value does not exist here..... ???
return $val.' :: '.$value ;
})
);
}
the return should be like 1a,1b,1c.
but the variable is null inside the getFormatter scope...
Thanks
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Its because of how PHP's scoping works. With most sensible languages (much as I love PHP it has its quirks!) that would indeed work (particularly if you are used to Javascript), but in PHP you have to explicitly tell it what variables to make in the closure's scope. That is done with the
use
statement - see the PHP documentation.Allan
Thanks Allan