Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include_once __DIR__ . '/Post.php';
- const DATA_FILE = __DIR__ . '/data/posts.txt';
- printPosts(getAllPosts());
- savePost(new Post('Html', "some text about html"));
- printPosts(getAllPosts());
- function getAllPosts() : array {
- $lines = file(DATA_FILE);
- $result = [];
- foreach ($lines as $line) {
- [$title , $text] = explode(';', trim($line));
- $result[] = new Post($title , $text);
- }
- return $result;
- }
- function savePost(Post $post) : void {
- $line = $post->title . ';' . $post->text . PHP_EOL;
- file_put_contents(DATA_FILE, $line, FILE_APPEND);
- }
- function printPosts(array $posts) {
- foreach ($posts as $post) {
- print $post . PHP_EOL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement