Site icon Leonid Mamchenkov

PHP assertions

When I hear the word “assertion”, the first thing that comes to my mind is PHPUnit assertions.  Sure, I write assertions in my unit tests.  But is that the only application?  Today I decided to figure it out, or, at least, learn more about the subject.

It turns out that PHP has assert() and assert_options() functions.  And those were there since the ancient times of PHP 4.  Sounds cool, but how useful are these?  Well, not that much:

Assertions should be used as a debugging feature only. You may use them for sanity-checks that test for conditions that should always be TRUE and that indicate some programming errors if not or to check for the presence of certain features like extension functions or certain system limits and features.

Assertions should not be used for normal runtime operations like input parameter checks. As a rule of thumb your code should always be able to work correctly if assertion checking is not activated.

This StackOverflow discussion expands a bit on the subject and concludes that assertions are just a developer tool used for troubleshooting and such. Bummer!

But I’m not that easily stopped.  Next stop – search for tools and libraries on GitHub and Packagist.  There’s more luck here!  A whole lot of different libraries exist that help with asserting facts and matching values to patterns.  I’ve checked a few of them and here’s the Top 3 List that I’m considering for use in my code:

I found a few more alternatives, but they looked like side projects or small toolboxes for specific needs.  None of those impressed me enough to be linked here.

It’s too late at night to make a decision right now on which project I like the most.  But I will definitely play more with the ones above.  If you have any experience with those or with any other assertion/matching library, I’m interested to hear.

 

Exit mobile version