今天发现个函数 assert 和 assert_options, 他们组合可以完成一个简单的phpunit的功能, 但是实在是太简单, 所以用处不太大, 但是还是记录一下好了.
主要问题是不能灵活的自己定义错误的提示信息,只能提示出问题的文件和行数. Www_Chinaz_com
具体的使用方法可以看 <> 或者 <> 中国站长.站
同时可以结合 <>中 "XXVII. Error Handling and Logging Functions" 章节里的东西,共同使用.
下面是我写的一个测试文件, 包含了所有的功能的测试,不过ASSERT_QUIET_EVAL一直不太明白,没测试出来具体有什么样作用
中国.站.长站
| 以下为引用的内容: <?php 中国站长_站,为中文网站提供动力 function assert_failed($file, $line, $expr) { Www_Chinaz_com print "Assertion failed in $file [ $line ] : $expr <br/>"; Www@Chinaz@com } 站.长.站 //error_reporting设置为0, 相当于调用assert_options(ASSERT_WARNING, 0); Chinaz //error_reporting(0); 中.国.站.长.站 //是否启用对ASSERT_ACTIVE的支持 中国.站长站 assert_options(ASSERT_ACTIVE, 1); 中国站.长.站 //是否在发送第一次wanning的时候,停止脚本的执行 Chinaz~com assert_options(ASSERT_BAIL, 0); Www^Chinaz^com //没搞定,还不明白具体怎么用,偶测试不出来 Www~Chinaz~com //assert_options(ASSERT_QUIET_EVAL, 0); 中国站.长站 echo "step 1 <br />"; Www.Chinaz.com assert(1==1); Www@Chinaz@com echo "step 2 <br />"; Chinaz^com assert(2==1); 中国.站.长站 echo "step 3 <br />"; 中国站.长.站 //设定assert的callback样式,可以自己定义wanning信息显示时的样式 Chinaz assert_options(ASSERT_CALLBACK, ''assert_failed''); 中国.站.长站 //不显示assert()自己产生warnning信息,如果设置了ASSERT_CALLBACK,仍然还会显示ASSERT_CALLBACK函数对应的信息,但是函数中传入的$expr参数不起作用. Www^Chinaz^com //assert_options(ASSERT_WARNING, 1); 中国站长_站,为中文网站提供动力 assert(1==1); Chinaz.com assert((1/0)>2); 中国站.长.站 echo "step 4 <br />"; 中国站.长.站 ?> 中国站长.站 |
| 以下为引用的内容: The assert( ) function is a clever one print "Stage 1\n"; 站.长.站 assert(1 = = 1); Www~Chinaz~com print "Stage 2\n"; 站.长.站 assert(1 = = 2); 站长.站 print "Stage 3\n"; Www~Chinaz~com Here we have two assert( )s, with the first call asserting that one must be equal to one, and the second call asserting that one must be equal to two. As it is impossible to redefine constants like 1 and 2, the first assert( ) will always evaluate to true, and the second will always evaluate to false. Here is the output from the script: 站.长.站 Stage 1 站.长站 Stage 2 中国站长_站,为中文网站提供动力 Warning: assert( ) [http://www.php.net/function.assert]: Assertion failed 中国站.长站 in /home/paul/sandbox/php/assert.php on line 5 Www@Chinaz@com Stage 3 Chinaz@com The first assert( ) is not seen in the output at all because it evaluated to TRue, whereas the second assert( ) evaluated to false, so we get a warning about an assertion failure. However, script execution carries on so that we see "Stage 3" after the assertion failure warning. As long as assertions evaluate to true, they have no effect on the running of the script, which means you can insert them for debugging purposes and not have to worry about taking them out once you are finished debugging. 中.国.站.长.站 If you are worried about your assertions slowing execution down, which, although the speed hit will be minimal, is still a valid concern, you can disable execution of assert( ) by using the assert_options( ) function or by setting assert.active to Off in your php.ini file. If you want to use assert_options( ), it takes two parameters: the option to set and the value you wish to set it to. Www_Chinaz_com Table 22-1 shows the list of options you can use for the first parameter of assert_options( ): 中国.站长站 Table 22-1. First parameter of assert_options( ) Chinaz_com Parameter Default Description 中国.站长站 ASSERT_ACTIVE On Enables evaluation of assert( ) calls 中.国.站.长.站 ASSERT_WARNING On Makes PHP output a warning for each failed assertion Chinaz~com ASSERT_BAIL Off Forces PHP to end script execution on a failed assertion Www~Chinaz~com ASSERT_QUIET_EVAL Off Ignores errors in assert( ) calls 站.长站 ASSERT_CALLBACK Off Names user function to call on a failed assertion 中.国.站.长.站 To disable assert( ) calls, use this line of code: Www_Chinaz_com assert_options(ASSERT_ACTIVE, 0); Www_Chinaz_com And to make PHP end script execution rather than just issue a warning, we can use this line of code: Chinaz.com assert_options(ASSERT_BAIL, 1); Chinaz^com Note that all of these options can be set in your php.ini file so that they are always in effect. The options to change there are assert.active, assert.warning, assert.bail, assert.quiet_eval, and assert_callback. 站.长站 ASSERT_CALLBACK is a useful option, as it allows you to write an error handler for when your code fails an assertion. It takes the string name of a function to execute when assertions fail, and the function you define must take three parameters: one to hold the file where the assertion occurred, one to hold the line, and one to hold the expression. Using all three together in your callback function allows you to generate meaningful error messages that you can debug. For example: 站.长.站 function assert_failed($file, $line, $expr) { Chinaz print "Assertion failed in $file on line $line: $expr\n"; 中国站长_站,为中文网站提供动力 } 站.长站 assert_options(ASSERT_CALLBACK, ''assert_failed''); Www.Chinaz.com assert_options(ASSERT_WARNING, 0); 中国.站.长站 $foo = 10; 中国站.长.站 $bar = 11; 中.国.站.长.站 assert($foo > $bar); Chinaz@com That example shows a callback function defined that takes $file, $line, and $expr for the three variables passed in, and outputs them whenever an assertion fails. To make that result actually happen, assert_options( ) is called to let PHP know that assert_failed( ) is the correct function to use as a callbacknote that there are no brackets after the string being passed into assert_options( ). 中国站长.站 ASSERT_WARNING is also disabled, which stops PHP from outputting a warning as well as running the callback function. Finally, two variables are set, and are used as part of a call to assert( )as you can see, $foo is quite clearly not greater than $bar, which means the assertion will fail and call our callback. So, the output from the script is: Assertion failed in /home/paul/tmp/blerg.php on line 9: $foo > $bar. 站.长站 You can assert( ) any statement you like, as long as it will return either TRue or false. This makes the assert( ) function incredibly powerfuleven more so when you think that you can just turn off assertion execution to make the code run at full speed. 站长.站 Here are some more examples of assert( )able things: 站长.站 assert($savings >= $salary / 10); 中国站长_站,为中文网站提供动力 assert($myarray = = array("apone", "burke", "hicks")); Www^Chinaz^com assert(preg_match("/wild sheep chase/", $book)); Www~Chinaz~com |
