未加星标

PHP assertions

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

It turns out thatphp 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:

beberlei/assert simple to use library, with a respectable number of implemented assertions. It supports chained methods, lazy assertions, and iseasy to extend. (See this blog post , announcing version 2 a few years back.) Also, the fact that almost 300 projects depend on it, makes it an attractive choice. nilportugues/php-assert also an easyto use library, which offers even more assertions, grouped into a number of categories (generic, string, integer, float, array, date and time, object, and file upload). It’s not anywhere near as popular as the previous option, but that is probablyjust a question of time. peridot-php/leo a much more advanced assertion and matching library than the previous two options. In fact, so much more advanced, that it has a dedicated documentation website . This is understandable, as this library is a part of the Peridot BDD testing framework. It is easy to extend too, but I’m not sure yet that I need that level of complexity in my projects.

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.

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: PHPGitGitHub
分页:12
转载请注明
本文标题:PHP assertions
本站链接:http://www.codesec.net/view/532542.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(72)