Extend SplFileObject with the fread Method

PHP5 introduced the object-oriented file shell SplFileObject . I guess I just found out why hardly anyone uses it. This is purely linear oriented, there is no fread () method, which seemed to offer the appropriate frwite.

So now I am wondering if I can extend it. However, I'm not sure which is the best workaround. SplFileObject is not particularly useful when laying out a file pointer ($ fp) - it is either a private attribute or an internal ZE resource. So how can I extend it to get the fread method?

  • Implement fread () as a string that collects a loop using fgets () , with a possible search if only a certain size was requested, and fgets reads too far until the next line \ n breaks.
  • Use a special constructor that opens the file a second time and saves the pointer to the secondary file if I want to call fread () instead of the methods of the parent class.

I would go for the fgets workaround cycle, but it sounds bulky and slow and provides more potential traps. OTOH, which opens the secondary file pointer, is also not suitable, and it needs its own destructor.

+3
source share
2 answers

fread, fgetc , .

, , . . - , fread .

+5

​​ PHP5.6

. ​​ # 65545 (SplFileObject:: fread()) (Tjerk)

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

+2

Source: https://habr.com/ru/post/1769431/


All Articles