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 '