Advertisement
toorr2p

Untitled

Feb 17th, 2024
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.54 KB | None | 0 0
  1. <?php
  2. require_once 'cached.php';
  3. include_once('../../pdo/CDatabase.php');
  4. include_once('../../pdo/CQuery.php');
  5.  
  6. class StaticPage extends Cached
  7. {
  8.     // Конструктор класса
  9.     public function __construct(protected ?int $id)
  10.     {
  11.  
  12.         // Есть, инициализируем объект содержимым кэша
  13.         parent::__construct($this->title(), $this->content());
  14.  
  15.         // Проверяем, нет ли такой страницы в кэше
  16.         if ($this->isCached($this->id($id))) {
  17.             // Есть, инициализируем объект содержимым кэша
  18.             //parent::__construct($this->title(), $this->content());
  19.         } else {
  20.             // Данные пока не кэшированы, извлекаем
  21.             // содержимое из базы данных
  22.  
  23.             /*
  24.             CREATE TABLE `static_pages` (`ID` int NOT NULL AUTO_INCREMENT,
  25.             `TITLE` varchar(255) NOT NULL,
  26.             `CONTENT` TEXT NULL,
  27.             `DATE_INSERT` TIMESTAMP NOT NULL,
  28.              PRIMARY KEY (`ID`)
  29.             );
  30.  
  31.             INSERT INTO `static_pages` (`TITLE`, `CONTENT`) VALUES ('Главная страница', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.');
  32.             */
  33.  
  34.             $dbh = new CDatabase('php8db', 'php8u', 'qwerty');
  35.             $query = new CQuery(
  36.                 'SELECT * FROM `static_pages` WHERE id = :id LIMIT 1',
  37.                 [$id],
  38.                 $dbh
  39.             );
  40.  
  41.             if ($page = $query->fetch()) {
  42.                 var_dump($page);
  43.                 echo '\n';
  44.  
  45.                 parent::__construct($page['TITLE'], $page['CONTENT']);
  46.  
  47.                 // Устанавливаем признак кэша страницы
  48.                 $this->set($this->id($id), 1);
  49.                 parent::__construct(
  50.                     $page['TITLE'],
  51.                     $page['CONTENT']
  52.                 );
  53.             }
  54.  
  55.         }
  56.     }
  57.  
  58.     // Уникальный ключ для кэша
  59.     public function id(mixed $name) : string
  60.     {
  61.       return "static_page_{$name}";
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement