Here's my anonymous function:
<td><?=call_user_func(function($x) { if ($x == 1) { echo $produto->retornaMedida($dadosProdutos[$t]->medida_id); } else if ($x == 2) { echo "N/A"; } }, $dadosProdutos[$t]->produto_id)?></td>
It works perfectly well if I don't use something "out of scope" inside the scope of the conditions, like this:
<td><?=call_user_func(function($x) { if ($x == 1) { echo "Whatever"; } else if ($x == 2) { echo "N/A"; } }, $dadosProdutos[$t]->produto_id)?></td>
But when I use $dadosProdutos
, for example, I get a:
[24-Dec-2015 03:47:58 America/Sao_Paulo] PHP Notice: Undefined variable: dadosProdutos in G:\Insanity\Web\xampp\htdocs\sisconbr-old\site\modulos\pedido\minhas-cotacoes.php on line 269
And $dadosProdutos is not "undefined" when used out of the anonymous function:
<td><?=$produto->retornaMedida($dadosProdutos[$t]->medida_id)?></td>
Interestingly, I have no problems when I pass the outside variable as an argument to the anonymous function. I think this is what they mean with "capturing an out-of-scope variable" on C++'s lambdas.