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

首頁 > 開發 > PHP > 正文

淺析動態網頁技術PHP中錯誤處理的一些方法

2024-05-04 21:52:46
字體:
來源:轉載
供稿:網友

已經玩php一段時間了,基本是from 0開始的,經常出現的問題也很多,慢慢的可以熟練的查詢文檔了,而且,經驗也不是很多。

先look here。:
 


error_reporting(1048);//1,2,4,7,...,1048
ob_start();
session_start();
date_default_timezone_set('Asia/Shanghai');
 


php的遞給調用會吃掉很多的內存。
一般的,不要太多的使用define。
在類的構造和屬性器方面,5已經做了很多的工作,克服的bug就不說了,__系列的函數都有一些看點,也是支持static的。

按照我現在的寫過的程序,很少用到框架頁面的,我感覺在php里面速度不在是一個問題。

注意類的調用次序,特別是已經extends的,準確的釋放掉內存。

 

@mysql_fetch_array(result,MYSQL_ASSOC);

這個注意使用亂用,MYSQL_ASSOC,MYSQL_NUM 和 MYSQL_BOTH是一個enum吧。默認的是MYSQL_BOTH.

mysql的語句,如果執行in 的話不再支持limit。so...很麻煩。

支持distinct,group之,程序循環執行,md。

 

if(get_magic_quotes_gpc()){
_GET= stripslashes_gpc(_GET);
_POST= stripslashes_gpc(_POST);
_COOKIE= stripslashes_gpc(_COOKIE);
}
set_magic_quotes_runtime(0);#magic_quotes_runtime
function stripslashes_gpc(array) {
while (list(key,value) = each(array)) {
if (is_string(value)) {
array[key] = addslashes(stripslashes(value));
} else if (is_array(value)) {
array[key] = stripslashes_gpc(value);
}
}
return array;
}
/**
* 處理程序中出現的錯誤
* @Date:2006-10-9-10:51
* @param (類型) (參數名) (描述)
*/
function Error_Handler(errno,errstr,errorfile,errline,errtext){
echo('系統發生錯誤:'.errstr);
echo('At'.errorfile.'的第'.errline.'行.');
if(errno==E_USER_ERROR){
echo('致命錯誤,程序已經中止');
}

}



在使用的時候:

set_error_handler('Error_Handler');
trigger_error('call trigger_error',E_USER_ERROR);
 



這是使用函數獲取的,當然您的錯誤報告級別應該高點:
 

error_reporting(1048);
 



為了更加oo,先看看一個描述類的自寫的函數:
 

classname='Exception';
/**
* (功能描述)
* @Date:
* @param (類型) (參數名) (描述)
*/
function ClassDetail(classname){
if(!class_exists(classname)){
echo(classname.'類不存在');
}else{
print_r("以下描述類".classname);
print_r("所有的方法<pre>");
print_r(get_class_methods(classname));
print_r("</pre>");
print_r("<hr/>所有的屬性<pre>");
print_r(get_class_vars(classname));
print_r("</pre><hr/>");
}
}
ClassDetail(classname);
 



發現exception類的所有get方法是:
 


[0] => __construct
[1] => getMessage
[2] => getCode
[3] => getFile
[4] => getLine
[5] => getTrace
[6] => getTraceAsString
[7] => __toString
 


慢慢的優化個性點,也可以的。



ok,來看比較oo的處理方式:
 

class ErrorHandlers extends Exception{
private _context = null;
function __construct(level, string, file, line, context=null){
parent::__construct(string,level);
this->file = file;
this->line = line;
this->_level = level;
this->_context = context;
}
function __destruct(){
// parent::__destruct();
}
function Message(){
errors = array(
E_ERROR => 'error',
E_WARNING => 'warning',
E_PARSE => 'parsing error',
E_NOTICE => 'notice',
E_CORE_ERROR => 'core error',
E_CORE_WARNING => 'core warning',
E_COMPILE_ERROR => 'compile error',
E_COMPILE_WARNING => 'compile warning',
E_USER_ERROR => 'user error',
E_USER_WARNING => 'user warning',
E_USER_NOTICE => 'user notice'
);


str = errors[parent::getCode()].': '.parent::getMessage().' 在 '.parent::getFile().
' 的第 '.parent::getLine()."行/n" ;

if(this->_level==E_USER_ERROR){
str .= ('<hr/>致命錯誤');
}
echo('<pre>');
echo(str);
echo('</pre>');

}
}
function error_handler(errno,errstr,errorfile,errline,errtext){
throw new ErrorHandlers(errno,errstr,errorfile,errline,errtext);

}
function exception_handler(Exception e)
{
errors = array(
E_ERROR => 'error',
E_WARNING => 'warning',
E_PARSE => 'parsing error',
E_NOTICE => 'notice',
E_CORE_ERROR => 'core error',
E_CORE_WARNING => 'core warning',
E_COMPILE_ERROR => 'compile error',
E_COMPILE_WARNING => 'compile warning',
E_USER_ERROR => 'user error',
E_USER_WARNING => 'user warning',
E_USER_NOTICE => 'user notice');

echo errors[e->getCode()].': '.e->getMessage().' in '.e->getFile().
' on line '.e->getLine()."/n";
echo e->getTraceAsString();
}

trigger_error('5do8');
try{
i = 1/0;
} catch(ErrorHandlers e) {
echo "發生錯誤."; //可以輸出錯誤行
e->Message();
}
 



而后,注意了,如果您第一次(或者重新)加載的話,就加上:
 

set_error_handler('error_handler');
set_exception_handler('exception_handler');
 



如果不是上述情況,就不要加了,否則會出現

 

Exception thrown without a stack frame in Unknown on line 0



因為error_handler是anto_flush的。

在一個exception里面不能調用其他的exception。有2條普遍適用的規則,如下:
 

1:不要在一個Exception里面執行另一個Exception
2:不要在析構函數里面執行Exception.
 




restore_exception_handler();是可以保存exception柄的,注意,執行error以后就會有Exception的了。

最后,加上一個完整的例子:CallError.php

<?
error_reporting(1048);
class ErrorHandlers extends Exception{
private _context = null;
function __construct(level, string, file, line, context=null){
parent::__construct(string,level);
this->file = file;
this->line = line;
this->_level = level;
this->_context = context;
}
function __destruct(){
// parent::__destruct();
}
function Message(){
errors = array(
E_ERROR => 'error',
E_WARNING => 'warning',
E_PARSE => 'parsing error',
E_NOTICE => 'notice',
E_CORE_ERROR => 'core error',
E_CORE_WARNING => 'core warning',
E_COMPILE_ERROR => 'compile error',
E_COMPILE_WARNING => 'compile warning',
E_USER_ERROR => 'user error',
E_USER_WARNING => 'user warning',
E_USER_NOTICE => 'user notice'
);


str = errors[parent::getCode()].': '.parent::getMessage().' 在 '.parent::getFile().
' 的第 '.parent::getLine()."行/n" ;

if(this->_level==E_USER_ERROR){
str .= ('<hr/>致命錯誤');
}
echo('<pre>');
echo(str);
echo('</pre>');

}
}
function error_handler(errno,errstr,errorfile,errline,errtext){
throw new ErrorHandlers(errno,errstr,errorfile,errline,errtext);

}
function exception_handler(Exception e)
{
errors = array(
E_ERROR => 'error',
E_WARNING => 'warning',
E_PARSE => 'parsing error',
E_NOTICE => 'notice',
E_CORE_ERROR => 'core error',
E_CORE_WARNING => 'core warning',
E_COMPILE_ERROR => 'compile error',
E_COMPILE_WARNING => 'compile warning',
E_USER_ERROR => 'user error',
E_USER_WARNING => 'user warning',
E_USER_NOTICE => 'user notice');

echo errors[e->getCode()].': '.e->getMessage().'在'.e->getFile().
'的第'.e->getLine()."行/n";
echo e->getMessage();
die();
}
//
set_error_handler('error_handler');
//restore_error_handler();
set_exception_handler('exception_handler');
//restore_exception_handler();

我肯定是錯誤
?>



執行結果:
 

notice: Use of undefined constant 我肯定是錯誤 - assumed '我肯定是錯誤'在E:/web/web/php/bi/exception/m.php的第74行 Use of undefined constant 我肯定是錯誤 - assumed '我肯定是錯誤'



學習動態網頁技術PHP中錯誤處理的一些方法

另外,在類中,還可以這樣:
 

function trigger_error(error_msg, error_type = E_USER_WARNING)
{
trigger_error(" error: error_msg", error_type);
}
 



著名的Smarty就是這么做的.
 

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大邑县| 新疆| 德江县| 霍林郭勒市| 双鸭山市| 龙南县| 霍城县| 巧家县| 兰州市| 达日县| 康保县| 商水县| 东明县| 渭源县| 常德市| 吉安市| 宁明县| 墨脱县| 黔西县| 云南省| 嘉义县| 建阳市| 塘沽区| 晋城| 平舆县| 阜阳市| 昆明市| 华阴市| 六盘水市| 顺昌县| 精河县| 姜堰市| 香格里拉县| 宁明县| 布拖县| 安仁县| 城市| 苗栗县| 南和县| 泸溪县| 郁南县|