Commit 5ab8e00d authored by Nikita Chernykh's avatar Nikita Chernykh
Browse files

Merge branch 'PTPS_Function_9' into 'main'

Ptps function 9

See merge request !9
parents 899adfa6 85c3a11d
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -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
+22 −0
Original line number Diff line number Diff line
@@ -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");
        }
    }
}