使用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