Advertisement
UniQuet0p1

Untitled

Oct 3rd, 2021
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. <?php
  2.  
  3. include_once __DIR__ . '/Post.php';
  4.  
  5. const DATA_FILE = __DIR__ . '/data/posts.txt';
  6.  
  7. printPosts(getAllPosts());
  8.  
  9. savePost(new Post('Html', "some text about html"));
  10.  
  11. printPosts(getAllPosts());
  12.  
  13. function getAllPosts() : array {
  14.  
  15. $lines = file(DATA_FILE);
  16.  
  17. $result = [];
  18. foreach ($lines as $line) {
  19. [$title , $text] = explode(';', trim($line));
  20.  
  21. $result[] = new Post($title , $text);
  22. }
  23.  
  24. return $result;
  25. }
  26.  
  27. function savePost(Post $post) : void {
  28.  
  29.  
  30. $line = $post->title . ';' . $post->text . PHP_EOL;
  31.  
  32. file_put_contents(DATA_FILE, $line, FILE_APPEND);
  33.  
  34. }
  35.  
  36. function printPosts(array $posts) {
  37. foreach ($posts as $post) {
  38. print $post . PHP_EOL;
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement