Page MenuHomePhorge

final class LinesOfALargeFile
libphutil Technical Documentation (Filesystem)

Read the lines of a file, one at a time. This allows you to process large files without holding them in memory. In most cases, it is more efficient (and certainly simpler) to read the entire file and explode() it. For more information, see LinesOfALarge. See also LinesOfALargeExecFuture, for a similar abstraction that works on executed commands.

foreach (new LinesOfALargeFile('/some/large/logfile.log') as $line) {
  // ...
}

If the file can not be read, a FilesystemException is thrown.

Tasks

Configuration

  • final public function setDelimiter($character) — Change the "line" delimiter character, which defaults to "\n". This is used to determine where each line ends.

Internals

  • protected function willRewind() — Closes the file handle if it is open, and reopens it.
  • protected function readMore() — Read the file chunk-by-chunk.
  • public function __destruct() — Closes the file handle.
  • private function closeHandle() — Close the file handle, if it is open.

Iterator Interface

  • final public function current()
  • final public function key()
  • final public function next()
  • final public function rewind()
  • final public function valid()

Construction

  • public function __construct($file_name) — To construct, pass the path to a file.

Other Methods

Methods

public function __get($name)
Inherited

This method is not documented.
Parameters
$name
Return
wild

public function __set($name, $value)
Inherited

This method is not documented.
Parameters
$name
$value
Return
wild

final public function current()
Inherited

This method is not documented.
Return
wild

final public function key()
Inherited

This method is not documented.
Return
wild

final public function next()
Inherited

This method is not documented.
Return
wild

final public function rewind()
Inherited

This method is not documented.
Return
wild

final public function valid()
Inherited

This method is not documented.
Return
wild

private function throwOnAttemptedIteration()
Inherited

This method is not documented.
Return
wild

public function getPhobjectClassConstant($key, $byte_limit)
Inherited

Phobject

Read the value of a class constant.

This is the same as just typing self::CONSTANTNAME, but throws a more useful message if the constant is not defined and allows the constant to be limited to a maximum length.

Parameters
string$keyName of the constant.
int|null$byte_limitMaximum number of bytes permitted in the value.
Return
stringValue of the constant.

final public function setDelimiter($character)
Inherited

LinesOfALarge

Change the "line" delimiter character, which defaults to "\n". This is used to determine where each line ends.

If you pass null, data will be read from source as it becomes available, without looking for delimiters. You can use this to stream a large file or the output of a command which returns a large amount of data.

Parameters
string|null$characterA one-byte delimiter character.
Return
this

protected function willRewind()

LinesOfALarge

Hook, called before rewind()(). Allows a concrete implementation to open resources or reset state.

LinesOfALargeFile

Closes the file handle if it is open, and reopens it.

Return
void

protected function readMore()

LinesOfALarge

Called when the iterator needs more data. The subclass should return more data, or empty string to indicate end-of-stream.

LinesOfALargeFile

Read the file chunk-by-chunk.

Return
stringNext chunk of the file.

public function __construct($file_name)

To construct, pass the path to a file.

Parameters
string$file_nameFile path to read.
Return
this//Implicit.//

public function __destruct()

Closes the file handle.

Return
void

private function closeHandle()

Close the file handle, if it is open.

Return
$this