SplFileObject::__construct

新しいファイルオブジェクトを作成する

説明

public SplFileObject::__construct(
    string $filename,
    string $mode = "r",
    bool $useIncludePath = false,
    resourcenull $context = null
)

新しいファイルオブジェクトを作成します。

パラメータ

filename

読み込むファイル。

ヒント

fopen wrappers が有効の場合、この関数のファイル名として URL を使用することができます。ファイル名の指定方法に関する詳細は fopen を参照ください。 サポートするプロトコル/ラッパー には、さまざまなラッパーの機能やその使用法、 提供される定義済み変数などの情報がまとめられています。

mode

ファイルをオープンするときのモード。許可されるモードのリストは fopen を参照。

useIncludePath

filename を探すのに include_path を探索するかどうか。

context

stream_context_create で作られる有効なコンテキストリソース。

エラー / 例外

filename がオープンできない場合、RuntimeException がスローされます。

filename がディレクトリの場合、LogicException がスローされます。

例1 SplFileObject::__construct の例

この例は現在のファイルをオープンし 1 行ごとに内容を反復処理します。

<?php
$file 
= new SplFileObject(__FILE__);
foreach (
$file as $line_num => $line) {
    echo 
"Line $line_num is $line";
}
?>

上の例の出力は、 たとえば以下のようになります。

Line 0 is <?php
Line 1 is $file = new SplFileObject(__FILE__);
Line 2 is foreach ($file as $line_num => $line) {
Line 3 is     echo "Line $line_num is $line";
Line 4 is }
Line 5 is ?>

参考

  • SplFileInfo::openFile
  • fopen