Loading index.php +12 −0 Original line number Diff line number Diff line Loading @@ -111,5 +111,17 @@ print $e->getMessage(); } ?> <h1>Function 9</h1> <?php try { foreach($func->readFileLineByLine(__DIR__ . "/public/text.txt") as $line) { print $line; } } catch (Exception $e) { print $e->getMessage(); } ?> </body> </html> No newline at end of file src/Functions.php +22 −0 Original line number Diff line number Diff line Loading @@ -207,5 +207,27 @@ class Functions throw new RuntimeException("File not found: $filePath"); } } /** * Переделай своё решение 8 задачи: * замени вывод всего текста из файла разом на * построчный вывод используя yield * @param string $filePath путь до файла * @return iterable */ public function readFileLineByLine(string $filePath): iterable { if (file_exists($filePath)) { $file = fopen($filePath, 'rb'); while(!feof($file)) { yield fgets($file); } fclose($file); } else { throw new RuntimeException("File not found: $filePath"); } } } Loading
index.php +12 −0 Original line number Diff line number Diff line Loading @@ -111,5 +111,17 @@ print $e->getMessage(); } ?> <h1>Function 9</h1> <?php try { foreach($func->readFileLineByLine(__DIR__ . "/public/text.txt") as $line) { print $line; } } catch (Exception $e) { print $e->getMessage(); } ?> </body> </html> No newline at end of file
src/Functions.php +22 −0 Original line number Diff line number Diff line Loading @@ -207,5 +207,27 @@ class Functions throw new RuntimeException("File not found: $filePath"); } } /** * Переделай своё решение 8 задачи: * замени вывод всего текста из файла разом на * построчный вывод используя yield * @param string $filePath путь до файла * @return iterable */ public function readFileLineByLine(string $filePath): iterable { if (file_exists($filePath)) { $file = fopen($filePath, 'rb'); while(!feof($file)) { yield fgets($file); } fclose($file); } else { throw new RuntimeException("File not found: $filePath"); } } }