【发布时间】:2011-04-05 14:18:01
【问题描述】:
使用 PHP,是否有一种函数/方法/方式来检查变量是否包含可以安全放入 foreach 构造的内容?类似的东西
//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 错误”,您的努力将受到赞赏,但我正在寻找其他东西。
【问题讨论】: