Read , Write file

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Read , Write file

Post by quocbao »

When open/read/write a file , we often use

Code: Select all

$fp = fopen($file , "rb");
$data = fread($fp,filesize($fp));
fclose($fp);
but i prefer

Code: Select all

$reader = new IO_FileReader($file);
$data = $reader->ReadToEnd();
$reader->close();
What do you think ?
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

The second method is better if you're worried about making user-friendly error handling and custom error handlers. The first method is better in terms of less code to manage, and is probably going to run faster because it's part of a precompiled built-in library.
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Post by quocbao »

Of course , the second method is better :D , we can also use exception handling instead of php's old-style error report .

This is similiar to StreamReader and StreamWriter in .NET :D
Post Reply