Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. <?php
  2.  
  3. class DataBase
  4. {
  5. private $db;
  6.  
  7. public function __construct()
  8. {
  9. $this->db = 'db.txt';
  10. }
  11. public function get($id)
  12. {
  13. $file = $this->readFromDb();
  14. return $file[$id];
  15. }
  16. public function insert($id,$value)
  17. {
  18. $file = $this->readFromDb();
  19. if (!isset($file[$id])) {
  20. $file[$id] = $value;
  21. $this->writeOnDb();
  22. }
  23.  
  24. }
  25. public function update($id,$value)
  26. {
  27. $file = $this->readFromDb();
  28. if (isset($file[$id])) {
  29. $file[$id] = $value;
  30. $this->writeOnDb();
  31. }
  32. }
  33. public function delete($id)
  34. {
  35. $file = $this->readFromDb();
  36. if(isset($file[$id])){
  37. unset($file[$id]);
  38. $this->writeOnDb();
  39. }
  40. }
  41. public function readFromDb()
  42. {
  43. return json_decode(file_get_contents($this->db),true);
  44. }
  45. public function writeOnDb()
  46. {
  47. return file_put_contents($this->db, json_enconde($this->readFromDb()));
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement