Utilizzo di PHP, esiste una funzione/metodo/modo per verificare se una variabile contiene qualcosa che sarebbe sicuro inserire in un costrutto foreach? Qualcosa di simileEsiste un modo per testare una variabile per "isForEachable"
//the simple case, would probably never use it this bluntly
function foo($things)
{
if(isForEachable($things))
{
foreach($things as $thing)
{
$thing->doSomething();
}
}
else
{
throw new Exception("Can't foreach over variable");
}
}
Se la vostra risposta è "l'installazione di un gestore per rilevare l'errore di PHP", i vostri sforzi sono apprezzati, ma sto cercando qualcosa di diverso.
correlati: http://stackoverflow.com/questions/3584700/iterable-objects-and-array-type- suggerimento/ – quantumSoup