国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHP自帶可以代替echo調(diào)試的unit函數(shù)

2024-05-04 23:03:25
字體:
供稿:網(wǎng)友


  今天發(fā)現(xiàn)個函數(shù) assert 和 assert_options, 他們組合可以完成一個簡單的phpunit的功能, 但是實在是太簡單, 所以用處不太大, 但是還是記錄一下好了.

  主要問題是不能靈活的自己定義錯誤的提示信息,只能提示出問題的文件和行數(shù).

  具體的使用方法可以看 <> 或者 <>

  同時可以結(jié)合 <>中 "xxvii. error handling and logging functions" 章節(jié)里的東西,共同使用.

  下面是我寫的一個測試文件, 包含了所有的功能的測試,不過assert_quiet_eval一直不太明白,沒測試出來具體有什么樣作用

<?php
function assert_failed($file, $line, $expr) {
    print "assertion failed in $file [ $line ] : $expr <br/>";
}
//error_reporting設(shè)置為0, 相當(dāng)于調(diào)用assert_options(assert_warning, 0);
//error_reporting(0);
//是否啟用對assert_active的支持
assert_options(assert_active, 1);
//是否在發(fā)送第一次wanning的時候,停止腳本的執(zhí)行
assert_options(assert_bail, 0);
//沒搞定,還不明白具體怎么用,偶測試不出來
//assert_options(assert_quiet_eval, 0);
echo "step 1 <br />";
assert(1==1);
echo "step 2 <br />";
assert(2==1);
echo "step 3 <br />";
//設(shè)定assert的callback樣式,可以自己定義wanning信息顯示時的樣式
assert_options(assert_callback, 'assert_failed');
//不顯示assert()自己產(chǎn)生warnning信息,如果設(shè)置了assert_callback,仍然還會顯示assert_callback函數(shù)對應(yīng)的信息,但是函數(shù)中傳入的$expr參數(shù)不起作用.
//assert_options(assert_warning, 1); 
assert(1==1);
assert((1/0)>2);
echo "step 4 <br />";
?>

  下面的一段話是直接從  中copy出來的

the assert( ) function is a clever one that works along the same lines as our print statements, but it only works if a certain condition is not matched. essentially, assert( ) is used to say "this statement must be trueif it isn't, please tell me." for example:
    print "stage 1/n";
    assert(1 =  = 1);
    print "stage 2/n";
    assert(1 =  = 2);
    print "stage 3/n";

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
    stage 3

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.
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( ) 
parameter          default    description
 
assert_active      on         enables evaluation of assert( ) calls
 
assert_warning     on         makes php output a warning for each failed assertion
 
assert_bail        off        forces php to end script execution on a failed assertion
 
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:
    assert_options(assert_active, 0);

and to make php end script execution rather than just issue a warning, we can use this line of code:
    assert_options(assert_bail, 1);

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) {
            print "assertion failed in $file on line $line: $expr/n";
    }
    assert_options(assert_callback, 'assert_failed');
    assert_options(assert_warning, 0);
    $foo = 10;
    $bar = 11;
    assert($foo > $bar);

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"));
    assert(preg_match("/wild sheep chase/", $book));

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁津县| 信宜市| 邳州市| 延吉市| 永靖县| 大厂| 那坡县| 晋江市| 宣威市| 潞城市| 永康市| 九江市| 财经| 长寿区| 开平市| 盐边县| 凌云县| 西林县| 景宁| 茶陵县| 万荣县| 盐津县| 蒲江县| 乐清市| 喀什市| 杭锦旗| 寻乌县| 海南省| 金川县| 明星| 安多县| 汉沽区| 天津市| 任丘市| 镇宁| 凤庆县| 房产| 金塔县| 大丰市| 浮梁县| 文登市|