PHPの出力バッファリング機能を使っていると、「バッファの内容を取得しつつ、同時にブラウザに出力したい」という場面に遭遇することがあります。そんな時に便利なのがob_get_flush関数です。
この記事では、ob_get_flush関数の基本的な使い方から実践的な応用例まで、分かりやすく解説します。
ob_get_flush関数とは?
ob_get_flush()
は、PHPの出力バッファリング関数の一つで、現在の出力バッファの内容を取得し、同時にブラウザに出力してからバッファを終了する関数です。
基本構文
string|false ob_get_flush()
- 戻り値: 出力バッファの内容(文字列)、またはバッファが有効でない場合は
false
- パラメータ: なし
他の関数との違い
ob_get_flush関数を理解するために、関連する関数との違いを整理しましょう。
関数の比較表
関数 | バッファ取得 | 出力 | バッファ削除 |
---|---|---|---|
ob_get_contents() | ✓ | ✗ | ✗ |
ob_get_clean() | ✓ | ✗ | ✓ |
ob_get_flush() | ✓ | ✓ | ✓ |
ob_end_flush() | ✗ | ✓ | ✓ |
処理の流れ
<?php
// ob_get_flush()は以下の処理を一度に実行
$content = ob_get_contents(); // バッファ内容を取得
ob_end_flush(); // バッファを出力して終了
return $content; // 取得した内容を返す
?>
基本的な使い方
シンプルな例
<?php
// 出力バッファリングを開始
ob_start();
echo "<h1>ページタイトル</h1>";
echo "<p>このコンテンツはバッファに保存されます。</p>";
echo "<p>現在時刻: " . date('Y-m-d H:i:s') . "</p>";
// バッファ内容を取得し、同時に出力
$bufferedContent = ob_get_flush();
// この時点で上記のHTMLがブラウザに表示される
echo "<hr>";
echo "<h2>取得したバッファ内容:</h2>";
echo "<pre>" . htmlspecialchars($bufferedContent) . "</pre>";
?>
実行結果の流れ
- バッファに蓄積されたHTML(h1, p要素)がブラウザに出力される
$bufferedContent
変数にも同じ内容が格納される- その後のechoで、取得した内容が再度表示される
実践的な活用例
1. ページ生成時間の計測
<?php
function renderPageWithTimer($pageContent) {
$startTime = microtime(true);
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>処理時間計測ページ</title>
</head>
<body>
<div class="content">
<?php echo $pageContent; ?>
</div>
<!-- フッター部分で処理時間を表示 -->
<footer>
<?php
$endTime = microtime(true);
$processingTime = round(($endTime - $startTime) * 1000, 2);
echo "<p>ページ生成時間: {$processingTime}ms</p>";
?>
</footer>
</body>
</html>
<?php
// ページ内容を取得して出力し、ログ用にも保存
$htmlContent = ob_get_flush();
// ログファイルに記録(オプション)
logPageGeneration($htmlContent, $processingTime);
return $htmlContent;
}
function logPageGeneration($content, $time) {
$logEntry = date('Y-m-d H:i:s') . " - 生成時間: {$time}ms - サイズ: " . strlen($content) . "bytes\n";
file_put_contents('page_generation.log', $logEntry, FILE_APPEND);
}
// 使用例
renderPageWithTimer("<h1>メインコンテンツ</h1><p>ここに動的なコンテンツが入ります。</p>");
?>
2. キャッシュ機能付きテンプレート
<?php
class TemplateCache {
private $cacheDir;
public function __construct($cacheDir = './cache/') {
$this->cacheDir = $cacheDir;
if (!is_dir($cacheDir)) {
mkdir($cacheDir, 0755, true);
}
}
public function render($templateName, $data = [], $cacheTime = 3600) {
$cacheFile = $this->cacheDir . $templateName . '.cache';
// キャッシュが有効かチェック
if (file_exists($cacheFile) && (time() - filemtime($cacheFile)) < $cacheTime) {
// キャッシュから読み込んで出力
$cachedContent = file_get_contents($cacheFile);
echo $cachedContent;
return $cachedContent;
}
// キャッシュが無効な場合、テンプレートを生成
ob_start();
$this->loadTemplate($templateName, $data);
// 内容を取得し、同時に出力
$content = ob_get_flush();
// キャッシュファイルに保存
file_put_contents($cacheFile, $content);
return $content;
}
private function loadTemplate($templateName, $data) {
extract($data); // 変数を展開
include "./templates/{$templateName}.php";
}
}
// 使用例
$cache = new TemplateCache();
$content = $cache->render('user_profile', [
'name' => '田中太郎',
'email' => 'tanaka@example.com',
'posts' => 42
], 1800); // 30分間キャッシュ
?>
3. プログレスバー付き長時間処理
<?php
function processLargeDataset($items) {
$total = count($items);
$processed = 0;
// 初期HTML出力
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>データ処理中</title>
<style>
.progress-bar { width: 100%; background: #f0f0f0; border-radius: 5px; }
.progress-fill { height: 20px; background: #4CAF50; border-radius: 5px; transition: width 0.3s; }
</style>
</head>
<body>
<h1>データ処理中...</h1>
<div class="progress-bar">
<div class="progress-fill" id="progress" style="width: 0%"></div>
</div>
<p id="status">処理開始...</p>
<script>
function updateProgress(percent, message) {
document.getElementById('progress').style.width = percent + '%';
document.getElementById('status').textContent = message;
}
</script>
</body>
</html>
<?php
// 初期表示を出力
$initialHtml = ob_get_flush();
// 各アイテムを処理
foreach ($items as $index => $item) {
// 実際の処理
processItem($item);
$processed++;
$percent = round(($processed / $total) * 100);
$message = "{$processed}/{$total} 件処理完了 ({$percent}%)";
// プログレス更新のJavaScriptを出力
echo "<script>updateProgress({$percent}, '{$message}');</script>";
flush(); // 即座にブラウザに送信
sleep(1); // デモ用の待機(実際の処理時間をシミュレート)
}
echo "<script>document.getElementById('status').textContent = '処理完了!';</script>";
return $initialHtml;
}
function processItem($item) {
// 実際のデータ処理をここに記述
// 例: データベース更新、ファイル処理など
}
// 使用例
$dataset = range(1, 10); // デモ用のデータセット
processLargeDataset($dataset);
?>
注意点とベストプラクティス
1. バッファレベルの確認
<?php
function safeObGetFlush() {
if (ob_get_level() > 0) {
return ob_get_flush();
} else {
error_log("警告: 出力バッファが開始されていません");
return false;
}
}
// 安全な使用例
ob_start();
echo "テストコンテンツ";
$content = safeObGetFlush();
if ($content !== false) {
echo "バッファ内容を正常に取得しました";
}
?>
2. エラーハンドリング
<?php
function renderWithErrorHandling($templateFile, $data = []) {
try {
ob_start();
if (!file_exists($templateFile)) {
throw new Exception("テンプレートファイルが見つかりません: {$templateFile}");
}
extract($data);
include $templateFile;
return ob_get_flush();
} catch (Exception $e) {
// エラーが発生した場合、バッファをクリーンアップ
if (ob_get_level() > 0) {
ob_end_clean();
}
// エラーページを表示
echo "<div class='error'>エラー: " . htmlspecialchars($e->getMessage()) . "</div>";
return false;
}
}
?>
3. メモリ使用量の考慮
<?php
// 大量のデータを扱う場合の注意
function processLargeContent($dataSize) {
// メモリ使用量をチェック
$memoryBefore = memory_get_usage();
ob_start();
// 大量のデータ生成(例)
for ($i = 0; $i < $dataSize; $i++) {
echo str_repeat("大量のテキストデータ", 100);
}
$memoryAfter = memory_get_usage();
$memoryUsed = $memoryAfter - $memoryBefore;
if ($memoryUsed > 10 * 1024 * 1024) { // 10MB以上
error_log("警告: 出力バッファが {$memoryUsed} バイトのメモリを使用しています");
}
return ob_get_flush();
}
?>
パフォーマンスに関する考慮事項
メリット
- 一石二鳥: 出力と内容取得を同時に実行
- メモリ効率: バッファを保持し続けない
- リアルタイム出力: ユーザーに即座に結果を表示
デメリット
- 制御の制約: 一度出力すると内容を変更できない
- デバッグの困難さ: 出力後の内容確認が難しい
- エラー処理: 出力後のエラー対応が複雑
適切な使用場面
ob_get_flush()が適している場面
- プログレスバー付きの長時間処理
- リアルタイムログ表示
- ストリーミング的な出力が必要な場合
- キャッシュ生成と同時表示
ob_get_clean()が適している場面
- テンプレートエンジンの実装
- 条件付き出力制御
- エラー時の出力キャンセル
- メール本文生成
まとめ
ob_get_flush()
関数は、出力バッファの内容取得と即座の出力を同時に行う便利な関数です。特にリアルタイムな出力が必要な場面や、処理結果を即座にユーザーに表示しつつログ記録も行いたい場合に威力を発揮します。
主な活用場面:
- プログレス表示: 長時間処理の進捗をリアルタイム表示
- キャッシュシステム: 生成と出力を同時実行
- ログ機能: 表示内容を記録用途にも活用
- パフォーマンス測定: ページ生成時間の計測
適切に使用することで、ユーザーエクスペリエンスの向上とシステムの効率化を両立できる優秀な関数です。ただし、一度出力した内容は変更できないため、エラーハンドリングには特に注意を払って実装しましょう。