使用PHP,有沒有一個函數/方法/方法來檢查變量是否包含可以安全地放入foreach構造的東西?就像有沒有辦法測試變量「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");
}
}
如果你的答案是「設置處理程序來捕捉PHP錯誤」,你的努力是讚賞,但我正在尋找別的東西。
相關:http://stackoverflow.com/questions/3584700/iterable-objects-and-array-type- hinting/ – quantumSoup 2010-08-30 04:10:36