首頁 > php教程 > php手册 > 主體

[PHP] 自訂錯誤處理

WBOY
發布: 2016-08-08 08:49:47
原創
1128 人瀏覽過

关闭掉默认的错误提示,注册自己的错误提示

Application.php

<?<span style="color: #000000;">php
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Application{
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> main(){
        </span><span style="color: #008080;">header</span>("content-type:text/html;charset=utf-8"<span style="color: #000000;">);
        self</span>::<span style="color: #000000;">register();
        self</span>::<span style="color: #000000;">router();
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">自动加载</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> loadClass(<span style="color: #800080;">$class</span><span style="color: #000000;">){
        </span><span style="color: #800080;">$class</span>=<span style="color: #008080;">str_replace</span>('\\', '/', <span style="color: #800080;">$class</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$class</span>="./".<span style="color: #800080;">$class</span>.".php"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">require_once</span> <span style="color: #800080;">$class</span><span style="color: #000000;">;        
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">pathinfo处理</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> router(){
        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_SERVER</span>['PATH_INFO'<span style="color: #000000;">])){
            </span><span style="color: #800080;">$pathinfo</span>=<span style="color: #008080;">array_filter</span>(<span style="color: #008080;">explode</span>("/", <span style="color: #800080;">$_SERVER</span>['PATH_INFO'<span style="color: #000000;">]));
            </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=1;<span style="color: #800080;">$i</span><=<span style="color: #008080;">count</span>(<span style="color: #800080;">$pathinfo</span>);<span style="color: #800080;">$i</span>++<span style="color: #000000;">){
                </span><span style="color: #800080;">$key</span>=<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$pathinfo</span>[<span style="color: #800080;">$i</span>]) ? <span style="color: #800080;">$pathinfo</span>[<span style="color: #800080;">$i</span>] : ''<span style="color: #000000;">;
                </span><span style="color: #800080;">$value</span>=<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$pathinfo</span>[<span style="color: #800080;">$i</span>+1]) ? <span style="color: #800080;">$pathinfo</span>[<span style="color: #800080;">$i</span>+1] :""<span style="color: #000000;">;
                </span><span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$i</span><span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">case</span> 1:
                        <span style="color: #800080;">$_GET</span>['m']=<span style="color: #008080;">ucfirst</span>(<span style="color: #800080;">$key</span><span style="color: #000000;">);
                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                    </span><span style="color: #0000ff;">case</span> 2:
                        <span style="color: #800080;">$_GET</span>['c']=<span style="color: #008080;">ucfirst</span>(<span style="color: #800080;">$key</span><span style="color: #000000;">);
                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                    </span><span style="color: #0000ff;">case</span> 3:
                        <span style="color: #800080;">$_GET</span>['a']=<span style="color: #800080;">$key</span><span style="color: #000000;">;
                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                    </span><span style="color: #0000ff;">default</span>:
                        <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$i</span>>3<span style="color: #000000;">){
                            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$i</span>%2==0<span style="color: #000000;">){
                                </span><span style="color: #800080;">$_GET</span>[<span style="color: #800080;">$key</span>]=<span style="color: #800080;">$value</span><span style="color: #000000;">;
                            }
                        }
                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                }
            }
        }
        </span><span style="color: #800080;">$_GET</span>['m']=!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_GET</span>['m']) ? <span style="color: #008080;">ucfirst</span>(<span style="color: #800080;">$_GET</span>['m']) : 'Index'<span style="color: #000000;">;
        </span><span style="color: #800080;">$_GET</span>['c']=!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_GET</span>['c']) ? <span style="color: #008080;">ucfirst</span>(<span style="color: #800080;">$_GET</span>['c']) : 'Index'<span style="color: #000000;">;
        </span><span style="color: #800080;">$_GET</span>['a']=!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_GET</span>['a']) ? <span style="color: #800080;">$_GET</span>['a'] : 'index'<span style="color: #000000;">;
        </span><span style="color: #800080;">$class</span>="\\Controller\\{<span style="color: #800080;">$_GET</span>['m']}\\{<span style="color: #800080;">$_GET</span>['c']}"<span style="color: #000000;">;
        </span><span style="color: #800080;">$controller</span>=<span style="color: #0000ff;">new</span> <span style="color: #800080;">$class</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$controller</span>-><span style="color: #800080;">$_GET</span>['a'<span style="color: #000000;">]();
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">致命错误回调</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> shutdownCallback(){
        </span><span style="color: #800080;">$e</span>=<span style="color: #000000;">error_get_last();
        </span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$e</span>) <span style="color: #0000ff;">return</span><span style="color: #000000;">;
        self</span>::errorHandler(<span style="color: #800080;">$e</span>['type'],'<font color="red">Fatal Error</font> '.<span style="color: #800080;">$e</span>['message'],<span style="color: #800080;">$e</span>['file'],<span style="color: #800080;">$e</span>['line'<span style="color: #000000;">]);
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">错误处理</span>
    <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> myErrorHandler(<span style="color: #800080;">$errno</span>,<span style="color: #800080;">$errstr</span>,<span style="color: #800080;">$errfile</span>,<span style="color: #800080;">$errline</span><span style="color: #000000;">){
        </span><span style="color: #0000ff;">list</span>(<span style="color: #800080;">$micseconds</span>,<span style="color: #800080;">$seconds</span>)=<span style="color: #008080;">explode</span>(" ",<span style="color: #008080;">microtime</span><span style="color: #000000;">());
        </span><span style="color: #800080;">$micseconds</span>=<span style="color: #008080;">round</span>(<span style="color: #800080;">$micseconds</span>*1000<span style="color: #000000;">);
        </span><span style="color: #800080;">$micseconds</span>=<span style="color: #008080;">strlen</span>(<span style="color: #800080;">$micseconds</span>)==1 ? '0'.<span style="color: #800080;">$micseconds</span> : <span style="color: #800080;">$micseconds</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$mes</span>="[ ".<span style="color: #008080;">date</span>("Y-m-d H:i:s",<span style="color: #800080;">$seconds</span>).":{<span style="color: #800080;">$micseconds</span>} ] ".<span style="color: #800080;">$errfile</span>." ".<span style="color: #800080;">$errline</span>." line ".<span style="color: #800080;">$errstr</span>.'<br/>'<span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$mes</span><span style="color: #000000;">;        
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">注册</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> register(){
        </span><span style="color: #008080;">error_reporting</span>(0<span style="color: #000000;">);
        </span><span style="color: #008080;">set_error_handler</span>(<span style="color: #0000ff;">function</span>(<span style="color: #800080;">$errno</span>,<span style="color: #800080;">$errstr</span>,<span style="color: #800080;">$errfile</span>,<span style="color: #800080;">$errline</span><span style="color: #000000;">){
            self</span>::myErrorHandler(<span style="color: #800080;">$errno</span>,<span style="color: #800080;">$errstr</span>,<span style="color: #800080;">$errfile</span>,<span style="color: #800080;">$errline</span><span style="color: #000000;">);
        });
        </span><span style="color: #008080;">register_shutdown_function</span>(<span style="color: #0000ff;">function</span><span style="color: #000000;">(){
            self</span>::<span style="color: #000000;">shutdownCallback();
        });
        spl_autoload_register(</span>"self::loadClass"<span style="color: #000000;">);
    }
}

Application</span>::main();
登入後複製

\Controller\Index\index.php

<?<span style="color: #000000;">php
namespace Controller\Index;

</span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Service\User;
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Index{
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct(){
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> index(){
        </span>0/0<span style="color: #000000;">;
        </span><span style="color: #008080;">mysql_connect</span><span style="color: #000000;">();
        </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xxx();
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> login(){
        </span><span style="color: #0000ff;">echo</span> "login()"<span style="color: #000000;">;
    }
}</span>
登入後複製

效果:

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!