Why does this work? I thought variables would only be visible within scope... Or is the scope method?
1) Ideally: $variable-name would be defined first step with foreach... but how?
2) why is $variable defined in the if clause accessible in the whole foreach block?
3) in PHPStorm I get an compile error, but the code works...
ArrayObject __construct:
// read JSON
foreach ($jsonIterator as $key => $val) {
if ($jsonIterator->getDepth()===0){
$variable = new Preguntas_Educacion_V1($key);
$this->offsetSet($variable->getColumn(),$variable);
} else if ($jsonIterator->getDepth()===1){
//Reflection!!! call setter dynamically by Val
$function="set".ucfirst($key);
$variable->$function($val);
} else if ($jsonIterator->getDepth()===2){
//Respuestas array
}
$counter++;
}