Should I be using assert in my PHP code?

Posted by Darryl Hein on Stack Overflow See other posts from Stack Overflow or by Darryl Hein
Published on 2010-12-23T06:33:37Z Indexed on 2010/12/23 6:54 UTC
Read the original article Hit count: 220

Filed under:
|

A co-worker has added the assert command a few times within our libraries in places where I would have used an if statement and thrown an exception. (I had never even heard of assert before this.) Here is an example of how he used it:

assert('isset($this->records); /* Records must be set before this is called. */');

I would have done:

if ( ! isset($this->records) {
    throw new Exception('Records must be set before this is called');
}

From reading the PHP docs on assert, it looks like it's recommended that make sure assert is active and add a handler before using assert. I can't find a place where he's done this.

So, my question is, is using assert a good idea given the above and should I be using it more often instead of if's and exceptions?

© Stack Overflow or respective owner

Related posts about php

Related posts about assert