「PHPの基礎 - インクルード文」の版間の差分

ナビゲーションに移動 検索に移動
編集の要約なし
(ページの作成:「== 概要 == 以下に示す関数を総称して、インクルード文またはインクルード系関数と呼ぶ。<br> これらの関数が共通して外部のPHPファイルを含める (include) という動作を行うことに由来している。<br> * require関数 * require_once関数 * include関数 * include_once関数 <br> require関数とinclude関数の基本的な違いとして、<br> require関数は、致命的なエラー (Fatal Error) を…」)
 
編集の要約なし
2行目: 2行目:
以下に示す関数を総称して、インクルード文またはインクルード系関数と呼ぶ。<br>
以下に示す関数を総称して、インクルード文またはインクルード系関数と呼ぶ。<br>
これらの関数が共通して外部のPHPファイルを含める (include) という動作を行うことに由来している。<br>
これらの関数が共通して外部のPHPファイルを含める (include) という動作を行うことに由来している。<br>
<br>
* require関数
* require関数
* require_once関数
* require_once関数
7行目: 8行目:
* include_once関数
* include_once関数
<br>
<br>
require関数とinclude関数の基本的な違いとして、<br>
require関数とinclude関数の基本的な違いとして、require関数は、致命的なエラー (Fatal Error) を発生させて、プログラムの実行を停止する。<br>
require関数は、致命的なエラー (Fatal Error) を発生させて、プログラムの実行を停止する。<br>
一方、include関数は、警告 (Warning) を発生させるだけで、プログラムは続行される。<br>
一方、include関数は、警告 (Warning) を発生させるだけで、プログラムは続行される。<br>
<br>
そのため、必須のファイル (設定ファイルやクラス定義等) にはrequire関数、オプショナルなファイル (テンプレート等) にはinclude関数を使用する。<br>
そのため、必須のファイル (設定ファイルやクラス定義等) にはrequire関数、オプショナルなファイル (テンプレート等) にはinclude関数を使用する。<br>
<br>
<br>
44行目: 45行目:
  <syntaxhighlight lang="php">
  <syntaxhighlight lang="php">
  // 推奨される方法
  // 推奨される方法
  if (file_exists('config.php')) {
   
     require_once 'config.php';
try {
    if (!file_exists('config.php')) {
      throw new Exception('設定ファイルが見つからない: config.php');
    }
     require_once 'config.php';  
  }
  }
  else {
  catch (Exception $e) {
     // 適切なエラーハンドリング
     // エラーログへの記録
     throw new Exception('設定ファイルが見つからない');
     error_log($e->getMessage());
    // 開発環境の場合は詳細なエラー情報を表示
    if (ENVIRONMENT === 'development') {
      echo 'エラーが発生: ' . $e->getMessage();
      echo '<br>スタックトレース: <pre>' . $e->getTraceAsString() . '</pre>';
    }
    else {
      // 本番環境では一般ユーザ向けのエラーページを表示
      require_once 'templates/error_page.php';
    }
    // 必要に応じて管理者にメール通知
    if (is_critical_error($e)) {
      notify_admin($e->getMessage());
    }
  }
  }
  </syntaxhighlight>
  </syntaxhighlight>
62行目: 82行目:
<br>
<br>
実務では、@require関数や@require_once関数はあまり推奨されない。<br>
実務では、@require関数や@require_once関数はあまり推奨されない。<br>
代わりに、proper error handlingの実装が推奨される。<br>
代わりに、以下に示すような例外処理の実装が推奨される。<br>
<br>
<syntaxhighlight lang="php">
// カスタム例外クラスの定義
class FileNotFoundException extends Exception
{
    protected $fileName;
    public function __construct($fileName)
    {
      $this->fileName = $fileName;
      parent::__construct("ファイル '{$fileName}' が見つかりません");
    }
    public function getFileName()
    {
      return $this->fileName;
    }
}
// エラーハンドリングの実装
class ErrorHandler
{
    public static function handleFileInclusion($filePath)
    {
      if (!file_exists($filePath)) {
          throw new FileNotFoundException($filePath);
      }
      if (!is_readable($filePath)) {
          throw new Exception("ファイル '{$filePath}' は読み取り可能ではありません");
      }
      require_once $filePath;
    }
    public static function logError($exception)
    {
      $logMessage = sprintf("[%s] %s in %s on line %d", date('Y-m-d H:i:s'), $exception->getMessage(), $exception->getFile(), $exception->getLine());
      error_log($logMessage);
    }
}
</syntaxhighlight>
<br>
<syntaxhighlight lang="php">
// 使用例
try {
    ErrorHandler::handleFileInclusion('config.php');
}
catch (FileNotFoundException $e) {
    ErrorHandler::logError($e);
    // ファイルが見つからない場合の代替処理
    load_default_config();
}
catch (Exception $e) {
    ErrorHandler::logError($e);
    // その他のエラーの処理
    // ...略
}
</syntaxhighlight>
<br><br>
<br><br>


案内メニュー