Home  >  Article  >  php教程  >  [PHP] Custom error handling

[PHP] Custom error handling

WBOY
WBOYOriginal
2016-08-08 08:49:471144browse

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

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>

效果:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn