> php教程 > php手册 > [PHP] 사용자 정의 오류 처리

[PHP] 사용자 정의 오류 처리

WBOY
풀어 주다: 2016-08-08 08:49:47
원래의
1163명이 탐색했습니다.

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

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으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿