php5とphp7の例外処理機構(thinkphp5の例外処理の分析)

藏色散人
リリース: 2023-02-17 11:30:02
転載
3638 人が閲覧しました

この記事では主にphp5とphp7の例外処理の仕組み(thinkphp5の例外処理の分析)を紹介しますので、困っている方の参考になれば幸いです。

1.php の例外とエラー

他の言語では例外とエラーは異なりますが、PHP は独自のエラーが発生するとエラーをトリガーします。例外。さらに、ほとんどの場合、PHP はエラーをトリガーしてプログラムの実行を終了しますが、PHP5 では try catch にはエラーを処理する方法がありません。

php7 はエラーをキャプチャできます;

1.1 php5 エラー例外

// 1.异常处理
try{
  throw new Exception("Error Processing Request", 1);
}catch ( Exception $e){
  echo $e->getCode().'<br/>';
  echo $e->getMessage().'<br/>';
  echo $e->getLine().'<br/>';
  echo $e->getFile().'<br/>';
}

返回:

1
Error Processing Request
158
E:\phpwebenv\PHPTutorial\WWW\test\index.php

// 2.结果php错误处理机制
function MyErrorHandler($error,$errstr,$errfile,$errline){
echo '<b> Custom error:</b>'.$error.':'.$errstr.'<br/>';
echo "Error on line $errline in ".$errfile;
}
set_error_handler('MyErrorHandler',E_ALL|E_STRICT);
try{
// throw new Exception("Error Processing Request", 4);
  trigger_error('error_msg');
}catch ( Exception $e){
  echo $e->getCode().'<br/>';
  echo $e->getMessage().'<br/>';
  echo $e->getLine().'<br/>';
  echo $e->getFile().'<br/>';
}

结果:
Custom error:1024:error_msg
Error on line 164 in E:\phpwebenv\PHPTutorial\WWW\test\index.php

//3. 处理致命错误:脚本结束后执行
function shutdown_function(){
  $e = error_get_last();
  echo '<pre/>';
  var_dump($e);
}
register_shutdown_function('shutdown_function');
try{
// throw new Exception("Error Processing Request", 4);
// trigger_error('error_msg');
  fun();
}catch ( Exception $e){
  echo $e->getCode().'<br/>';
  echo $e->getMessage().'<br/>';
  echo $e->getLine().'<br/>';
  echo $e->getFile().'<br/>';
}

结果:

Fatal error: Uncaught Error: Call to undefined function fun() in E:\phpwebenv\PHPTutorial\WWW\
test\index.php:172 Stack trace: #0 {main} thrown in E:\phpwebenv\PHPTutorial\WWW\test\index.php on line 172
array(4) {
  ["type"]=>
  int(1)
  ["message"]=>
  string(131) "Uncaught Error: Call to undefined function fun() in E:\phpwebenv\PHPTutorial\WWW\test\index.php:172
Stack trace:
#0 {main}
  thrown"
  ["file"]=>
  string(43) "E:\phpwebenv\PHPTutorial\WWW\test\index.php"
  ["line"]=>
  int(172)
}
以上方法可以看出,php没有捕获到异常,只能依赖set_error_handler()和register_shutdown_function();来处理,set_error_handler只能接受
异常和非致命的错误。register_shutdown_function():主要针对die()或致命错误,即程序终结后执行;所以php5没有很好的异常处理机制。
ログイン後にコピー

1.2 php7 例外処理

// 处理致命错误:脚本结束后执行
function shutdown_function(){
    $e = error_get_last();
    echo '<pre class="brush:php;toolbar:false">';
    var_dump($e);
}
register_shutdown_function('shutdown_function');
// 结果php错误处理机制
function MyErrorHandler($error,$errstr,$errfile,$errline){
    echo '<b> Custom error:</b>'.$error.':'.$errstr.'<br/>';
    echo "Error on line $errline in ".$errfile;
}
set_error_handler('MyErrorHandler',E_ALL|E_STRICT);
try{
    // throw new Exception("Error Processing Request", 4);
    // trigger_error('error_msg');
    fun();
}catch ( Error $e){
    echo $e->getCode().'<br/>';
    echo $e->getMessage().'<br/>';
    echo $e->getLine().'<br/>';
    echo $e->getFile().'<br/>';
}
结果:
0
Call to undefined function fun()
172
E:\phpwebenv\PHPTutorial\WWW\test\index.php
NULL  
register_shutdown_function();没有捕获到异常
ログイン後にコピー
// 2. 如果不用try catch 捕获

function exception_handler( Throwable $e){

    echo &#39;catch Error:&#39;.$e->getCode().&#39;:&#39;.$e->getMessage().&#39;<br/>&#39;;


}

set_exception_handler(&#39;exception_handler&#39;);

fun();
ログイン後にコピー

概要: Throwable はエラーと例外の基本クラスです。php7 では、例外をキャッチしたい場合は、エラーをキャッチする必要があります。

try{
    fun();
}catch ( Throwable $e){
    echo $e->getCode().&#39;<br/>&#39;;
    echo $e->getMessage().&#39;<br/>&#39;;
    echo $e->getLine().&#39;<br/>&#39;;
    echo $e->getFile().&#39;<br/>&#39;;
}
ログイン後にコピー

3. thinkphp5 フレームワークのエラー処理:

 在异常错误处理类:Error有这个处理  
// 注册错误和异常处理机制
\think\Error::register();
 /**
     * 注册异常处理
     * @return void
     */
    public static function register()
    {
        error_reporting(E_ALL);
        set_error_handler([__CLASS__, &#39;appError&#39;]);
        set_exception_handler([__CLASS__, &#39;appException&#39;]);
        register_shutdown_function([__CLASS__, &#39;appShutdown&#39;]);
    }
当程序出现错误时,会执行这些异常、错误的函数;
ログイン後にコピー

データベースに接続した後、例外は次のように処理されます。

/**
     * 连接数据库方法
     * @access public
     * @param array         $config 连接参数
     * @param integer       $linkNum 连接序号
     * @param array|bool    $autoConnection 是否自动连接主数据库(用于分布式)
     * @return PDO
     * @throws Exception
     */
    public function connect(array $config = [], $linkNum = 0, $autoConnection = false)
    {
        if (!isset($this->links[$linkNum])) {
            if (!$config) {
                $config = $this->config;
            } else {
                $config = array_merge($this->config, $config);
            }
            // 连接参数
            if (isset($config[&#39;params&#39;]) && is_array($config[&#39;params&#39;])) {
                $params = $config[&#39;params&#39;] + $this->params;
            } else {
                $params = $this->params;
            }
            // 记录当前字段属性大小写设置
            $this->attrCase = $params[PDO::ATTR_CASE];

            // 数据返回类型
            if (isset($config[&#39;result_type&#39;])) {
                $this->fetchType = $config[&#39;result_type&#39;];
            }
            try {
                if (empty($config[&#39;dsn&#39;])) {
                    $config[&#39;dsn&#39;] = $this->parseDsn($config);
                }
                if ($config[&#39;debug&#39;]) {
                    $startTime = microtime(true);
                }
                $this->links[$linkNum] = new PDO($config[&#39;dsn&#39;], $config[&#39;username&#39;], $config[&#39;password&#39;], $params);
                if ($config[&#39;debug&#39;]) {
                    // 记录数据库连接信息
                    Log::record(&#39;[ DB ] CONNECT:[ UseTime:&#39; . number_format(microtime(true) - $startTime, 6) . &#39;s ] &#39; . $config[&#39;dsn&#39;], &#39;sql&#39;);
                }
            } catch (\PDOException $e) {
                if ($autoConnection) {
                    Log::record($e->getMessage(), &#39;error&#39;);
                    return $this->connect($autoConnection, $linkNum);
                } else {
                    throw $e;
                }
            }
        }
        return $this->links[$linkNum];
    }

当数据库链接失败后,可以重新链接或者直接抛出异常;

    /**
     * 析构方法
     * @access public
     */
    public function __destruct()
    {
        // 释放查询
        if ($this->PDOStatement) {
            $this->free();
        }
        // 关闭连接
        $this->close();
    }
当执行sql失败后,调用析构方法,关闭数据库链接;
ログイン後にコピー

4. PHP でエラーが発生すると、リソースが解放されます

#php は説明用のスクリプトであり、PHP の各ページは独立した実行プログラムであり、どのような方法で実行しても (die()、exit()、致命的エラー終了プログラムも含む)、結果が返されます。サーバーに送信すると閉じられます。プログラムが閉じられると、当然リソースは解放されます;

unset(); 複数の変数名またはオブジェクト名がストレージ アドレスを指している場合、unset() 関数の機能は、そのアドレスを破棄することだけです。変数名と格納アドレスのポインタ 以上です、変数名またはオブジェクト名が 1 つだけの場合、unset は指定された格納アドレスの内容を破棄します;

破棄方法: インスタンス化されたオブジェクトに他の変数がない場合またはそれを指すオブジェクト名がある場合、このメソッドが実行されます。または、スクリプトの終了後にオブジェクト リソースが解放されたときにこのメソッドが実行されます。

関連する推奨事項:

PHP7 と PHP5 のセキュリティの違い (例) )>>PHP7 の抽象構文ツリー (AST) によってもたらされる変更点 >>PHP7言語の実行原理(PHP7ソースコード解析)>>

PHP 7.4は2019年12月リリース予定>>

以上がphp5とphp7の例外処理機構(thinkphp5の例外処理の分析)の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:cnblogs.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!