mario
mario

Reputation: 145482

extend SplFileObject with fread method

PHP5 introduced an object-oriented file wrapper, SplFileObject. I guess I just found out why hardly anybody is using it. It's purely line-oriented, there is no fread() method - which the corresponding frwite seemed to suggest.

So now I'm wondering if I can extend it. I'm however not sure what's the better workaround. SplFileObject is not particular helpful in tucking the file pointer ($fp) away - it's either a private attribute or ZE-internal resource. So how would I extend it to get a fread method?

I'd go for the fgets workaround loop, but it sounds cumbersome and slow and provides for more potential pitfalls. OTOH opening a secondary file pointer seems not a good approach either and needs a custom destructor too.

Upvotes: 3

Views: 1670

Answers (2)

W4cko
W4cko

Reputation: 31

Feature added in PHP5.6

. Added feature #65545 (SplFileObject::fread()) (Tjerk)

From https://github.com/php/php-src/blob/php-5.6.0RC2/NEWS

Upvotes: 2

Artefacto
Artefacto

Reputation: 97835

You can call extend it and add an fread method that calls the fgetc method repeatedly, the desired number of times.

That said, I agree this is a weird limitation. Submit a feature request. If I'm not missing something, and fread method ought to be implemented.

Upvotes: 5

Related Questions