【发布时间】:2010-12-23 06:33:37
【问题描述】:
一位同事在我们的库中添加了几次assert 命令,在这些地方我会使用 if 语句并引发异常。 (在此之前我什至从未听说过 assert。)以下是他如何使用它的示例:
assert('isset($this->records); /* Records must be set before this is called. */');
我会做的:
if (!isset($this->records)) {
throw new Exception('Records must be set before this is called');
}
从阅读the PHP docs on assert 看来,建议您确保断言处于活动状态并在使用断言之前添加一个处理程序。我找不到他这样做的地方。
所以,我的问题是,鉴于上述情况,使用断言是一个好主意吗?我应该更频繁地使用它而不是 if 和异常吗?
另外注意,我们计划在各种项目和服务器上使用这些库,包括我们甚至可能不参与的项目(这些库是开源的)。这对使用断言有什么影响吗?
【问题讨论】:
-
真的是
'isset(assert的代码行)吗?不仅仅是isset(没有单引号,')?