From 31414c143344a98db3b60e49df65f7e82e17a2ac Mon Sep 17 00:00:00 2001 From: Igor Simdyanov Date: Sat, 21 May 2022 18:44:09 +0300 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BB=D0=B0=D0=B2=D0=B0=2030:=20=D0=BD?= =?UTF-8?q?=D0=B0=D1=81=D0=BB=D0=B5=D0=B4=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20(=D1=87=D0=B5=D1=80=D0=BD=D0=BE=D0=B2=D0=B8=D0=BA)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inherit/anonym.php | 15 +++++++ inherit/anonym_nested.php | 26 ++++++++++++ inherit/file/logger.php | 27 ++++++++++++ inherit/file/logger/debug.php | 31 ++++++++++++++ inherit/file/logger/debug0.php | 32 +++++++++++++++ inherit/final.php | 9 ++++ inherit/inherit0.php | 6 +++ inherit/inherit0cast.php | 14 +++++++ inherit/inherit_static.php | 21 ++++++++++ inherit/pages/cached.php | 75 ++++++++++++++++++++++++++++++++++ inherit/pages/cached_a.php | 18 ++++++++ inherit/pages/cast.php | 10 +++++ inherit/pages/instanceof.php | 14 +++++++ inherit/pages/news.php | 30 ++++++++++++++ inherit/pages/page.php | 34 +++++++++++++++ inherit/pages/page_a.php | 34 +++++++++++++++ inherit/pages/static_page.php | 30 ++++++++++++++ inherit/pages/test.php | 7 ++++ inherit/static.php | 22 ++++++++++ inherit/test.log | 0 20 files changed, 455 insertions(+) create mode 100644 inherit/anonym.php create mode 100644 inherit/anonym_nested.php create mode 100644 inherit/file/logger.php create mode 100644 inherit/file/logger/debug.php create mode 100644 inherit/file/logger/debug0.php create mode 100644 inherit/final.php create mode 100644 inherit/inherit0.php create mode 100644 inherit/inherit0cast.php create mode 100644 inherit/inherit_static.php create mode 100644 inherit/pages/cached.php create mode 100644 inherit/pages/cached_a.php create mode 100644 inherit/pages/cast.php create mode 100644 inherit/pages/instanceof.php create mode 100644 inherit/pages/news.php create mode 100644 inherit/pages/page.php create mode 100644 inherit/pages/page_a.php create mode 100644 inherit/pages/static_page.php create mode 100644 inherit/pages/test.php create mode 100644 inherit/static.php create mode 100644 inherit/test.log diff --git a/inherit/anonym.php b/inherit/anonym.php new file mode 100644 index 0000000..b3da032 --- /dev/null +++ b/inherit/anonym.php @@ -0,0 +1,15 @@ +title = 'Hello world!'; + } +}); diff --git a/inherit/anonym_nested.php b/inherit/anonym_nested.php new file mode 100644 index 0000000..f02ae0f --- /dev/null +++ b/inherit/anonym_nested.php @@ -0,0 +1,26 @@ +title) extends Container + { + + private $name; + + public function __construct($title) + { + $this->name = $title; + } + + public function print() + { + echo "{$this->name} ({$this->id})"; + } + }; + } +} + +(new Container)->anonym()->print(); diff --git a/inherit/file/logger.php b/inherit/file/logger.php new file mode 100644 index 0000000..9891107 --- /dev/null +++ b/inherit/file/logger.php @@ -0,0 +1,27 @@ +name = $name; + $this->f = fopen($fname, 'a+'); + } + + public function __destruct() + { + fputs($this->f, join('', $this->lines)); + fclose($this->f); + } + + public function log($str) + { + $prefix = '[' . date('Y-m-d_h:i:s ') . "{$this->name}] "; + $str = preg_replace('/^/m', $prefix, rtrim($str)); + $this->lines[] = $str . PHP_EOL; + } +} diff --git a/inherit/file/logger/debug.php b/inherit/file/logger/debug.php new file mode 100644 index 0000000..aac7ae5 --- /dev/null +++ b/inherit/file/logger/debug.php @@ -0,0 +1,31 @@ +log("[at $file line $line] $s"); + } + + // Все остальные методы и свойства наследуются автоматически! +} diff --git a/inherit/file/logger/debug0.php b/inherit/file/logger/debug0.php new file mode 100644 index 0000000..6505a0e --- /dev/null +++ b/inherit/file/logger/debug0.php @@ -0,0 +1,32 @@ +logger = new FileLogger($name, $fname); + // Здесь можно проинициализировать другие свойства текущего + // класса, если они будут + } + + // Добавляем новый метод + public function debug($s, $level = 0) + { + $stack = debug_backtrace(); + $file = basename($stack[$level]['file']); + $line = $stack[$level]['line']; + $this->logger->log("[at $file line $line] $s"); + } + + // Оставляем на месте старый метод log() + public function log($s) { return $this->logger->log($s); } + // И такие методы-посредники мы должны создать ДЛЯ КАЖДОГО + // метода из FileLogger +} diff --git a/inherit/final.php b/inherit/final.php new file mode 100644 index 0000000..7c236bf --- /dev/null +++ b/inherit/final.php @@ -0,0 +1,9 @@ +log('Обычное сообщение'); +$logger->debug('Отладочное сообщение'); diff --git a/inherit/inherit0cast.php b/inherit/inherit0cast.php new file mode 100644 index 0000000..e2274f2 --- /dev/null +++ b/inherit/inherit0cast.php @@ -0,0 +1,14 @@ +log($msg); + exit(); +} diff --git a/inherit/inherit_static.php b/inherit/inherit_static.php new file mode 100644 index 0000000..a50ce42 --- /dev/null +++ b/inherit/inherit_static.php @@ -0,0 +1,21 @@ +expires = $expires; + // Подготовка хранилища + // $this->store = new Memcached(); + // $this->store->addServer('localhost', 11211); + // Размещение данных в хранилище + $this->set($this->id('title'), $title); + $this->set($this->id('content'), $content); + } + + // Проверить, есть ли позиция $key в кэше + protected function isCached($key) + { + // return (bool) $this->store->get($key); + } + // Поместить в кэш по ключу $key значение $value. + // В случае если ключ уже существует: + // 1. Не делать ничего, если $force принимает значение false. + // 2. Переписать, если $force принимает значение true. + protected function set($key, $value, $force = false) + { + // if ($force) { + // $this->store->set($key, $value, $this->expires); + // } else { + // if($this->isCached($key)) { + // $this->store->set($key, $value, $this->expires); + // } + // } + } + // Извлечение значения $key из кэша + protected function get($key) + { + // return $this->store->get($key); + } + + // Формируем уникальный ключ для хранилища + public function id($name) + { + die('Что здесь делать? Неизвестно!'); + } + + // Получение заголовка страницы + public function title() + { + // if ($this->isCached($this->id('title'))) { + // return $this->get($this->id('title')); + // } else { + return parent::title(); + // } + } + // Получение содержимое страницы + public function content() + { + // if ($this->isCached($this->id('content'))) { + // return $this->get($this->id('content')); + // } else { + return parent::content(); + // } + } +} diff --git a/inherit/pages/cached_a.php b/inherit/pages/cached_a.php new file mode 100644 index 0000000..bfe4207 --- /dev/null +++ b/inherit/pages/cached_a.php @@ -0,0 +1,18 @@ +render(); +} + +$shape = new StaticPage(3); +echoPage($shape); diff --git a/inherit/pages/instanceof.php b/inherit/pages/instanceof.php new file mode 100644 index 0000000..4c68ce9 --- /dev/null +++ b/inherit/pages/instanceof.php @@ -0,0 +1,14 @@ +"); + } + $obj->render(); +} + +$page = new StaticPage(3); +echoPage($page); diff --git a/inherit/pages/news.php b/inherit/pages/news.php new file mode 100644 index 0000000..02e8592 --- /dev/null +++ b/inherit/pages/news.php @@ -0,0 +1,30 @@ +isCached($this->id($id))) { + // Есть, инициализируем объект содержимым кэша + parent::__construct($this->title(), $this->content()); + } else { + // Данные пока не кэшированы, извлекаем + // содержимое из базы данных + // $query = "SELECT * FROM news WHERE id = :id LIMIT 1" + // $sth = $dbh->prepare($query); + // $sth = $dbh->execute($query, [$id]); + // $page = $sth->fetch(PDO::FETCH_ASSOC); + // parent::__construct($page['title'], $page['title']); + parent::__construct('Новости', 'Содержимое страницы Новости'); + } + } + + // Уникальный ключ для кэша + public function id($name) + { + return "news_{$name}"; + } +} diff --git a/inherit/pages/page.php b/inherit/pages/page.php new file mode 100644 index 0000000..daf4d25 --- /dev/null +++ b/inherit/pages/page.php @@ -0,0 +1,34 @@ +title = $title; + $this->content = $content; + } + + // Получение заголовка страницы + public function title() + { + return $this->title; + } + + // Получение содержимого страницы + public function content() + { + return $this->content; + } + + // Формирование HTML-представления страницы + public function render() + { + echo '

' . htmlspecialchars($this->title()) . '

'; + echo '

' . nl2br(htmlspecialchars($this->content())) . '

'; + } +} diff --git a/inherit/pages/page_a.php b/inherit/pages/page_a.php new file mode 100644 index 0000000..99fd9c0 --- /dev/null +++ b/inherit/pages/page_a.php @@ -0,0 +1,34 @@ +title = $title; + $this->content = $content; + } + + // Получение заголовка страницы + public function title() + { + return $this->title; + } + + // Получение содержимого страницы + public function content() + { + return $this->content; + } + + // Формирование HTML-представления страницы + public function render() + { + echo '

' . htmlspecialchars($this->title()) . '

'; + echo '

' . nl2br(htmlspecialchars($this->content())) . '

'; + } +} diff --git a/inherit/pages/static_page.php b/inherit/pages/static_page.php new file mode 100644 index 0000000..a0a9c24 --- /dev/null +++ b/inherit/pages/static_page.php @@ -0,0 +1,30 @@ +isCached($this->id($id))) { + // Есть, инициализируем объект содержимым кэша + parent::__construct($this->title(), $this->content()); + } else { + // Данные пока не кэшированы, извлекаем + // содержимое из базы данных + // $query = "SELECT * FROM static_pages WHERE id = :id LIMIT 1" + // $sth = $dbh->prepare($query); + // $sth = $dbh->execute($query, [$id]); + // $page = $sth->fetch(PDO::FETCH_ASSOC); + // parent::__construct($page['title'], $page['title']); + parent::__construct('Контакты', 'Содержимое страницы Контакты'); + } + } + + // Уникальный ключ для кэша + public function id($name) + { + return "static_page_{$id}"; + } +} diff --git a/inherit/pages/test.php b/inherit/pages/test.php new file mode 100644 index 0000000..eb92383 --- /dev/null +++ b/inherit/pages/test.php @@ -0,0 +1,7 @@ +render(); +echo $page->id($id); diff --git a/inherit/static.php b/inherit/static.php new file mode 100644 index 0000000..042ab4a --- /dev/null +++ b/inherit/static.php @@ -0,0 +1,22 @@ +