Глава 36: переработка автозагрузки

This commit is contained in:
Igor Simdyanov
2022-05-28 12:01:16 +03:00
parent 8ef734e585
commit fc1ecdd722
7 changed files with 19 additions and 32 deletions

View File

@ -1,11 +1,11 @@
<?php <?php
namespace PHP8; namespace PHP8;
trait Tag trait Author
{ {
public function tags() public function authors()
{ {
// $query = 'SELECT * FROM authors WHERE id IN(:ids)' // $query = 'SELECT * FROM authors WHERE id IN(:ids)'
echo 'Tag::tags<br />'; echo 'Author::authors()<br />';
} }
} }

View File

@ -1,18 +1,14 @@
<?php <?php
namespace PHP8; namespace PHP8;
use \PHP8\Seo as Seo; use \PHP8\Seo as Seo;
use \PHP8\Tag as Tag; use \PHP8\Author as Author;
class Page class Page
{ {
use Seo, Tag; use Seo, Author;
protected $title; public function __construct(
protected $content; protected string $title = '',
protected string $content = ''
public function __construct(string $title = '', string $content = '') ) {}
{
$this->title = $title;
$this->content = $content;
}
} }

View File

@ -3,10 +3,6 @@ namespace PHP8;
trait Seo trait Seo
{ {
private $keyword;
private $description;
private $ogs;
public function keywords() public function keywords()
{ {
// $query = 'SELECT keywords FROM seo WHERE id = :id LIMIT 1' // $query = 'SELECT keywords FROM seo WHERE id = :id LIMIT 1'

View File

@ -1,8 +1,11 @@
<?php <?php
spl_autoload_register(function($classname){ spl_autoload_register(function(string $classname){
$path = strtolower($classname); $namespace = array_map(
require_once(__DIR__ . "/$path.php"); fn($class) => $class != 'PHP8' ? strtolower($class) : $class,
explode('\\', $classname)
);
require_once(__DIR__ . '/' . implode('/', $namespace) . '.php');
}); });
$page = new PHP8\Page('О нас', 'Содержимое страницы'); $page = new PHP8\Page('О нас', 'Содержимое страницы');
$page->tags(); $page->authors(); // Author::authors()

View File

@ -1,8 +0,0 @@
<?php
function __autoload($classname)
{
require_once(__DIR__ . "/$classname.php");
}
$page = new PHP8\Page('О нас', 'Содержимое страницы');
$page->tags();

View File

@ -2,4 +2,4 @@
spl_autoload_register(); spl_autoload_register();
$page = new PHP8\Page('О нас', 'Содержимое страницы'); $page = new PHP8\Page('О нас', 'Содержимое страницы');
$page->tags(); // Tag::tags $page->authors(); // Author::authors()

View File

@ -1,7 +1,7 @@
<?php <?php
require_once(__DIR__ . '/PHP8/seo.php'); require_once(__DIR__ . '/PHP8/seo.php');
require_once(__DIR__ . '/PHP8/tag.php'); require_once(__DIR__ . '/PHP8/author.php');
require_once(__DIR__ . '/PHP8/page.php'); require_once(__DIR__ . '/PHP8/page.php');
$page = new PHP8\Page('О нас', 'Содержимое страницы'); $page = new PHP8\Page('О нас', 'Содержимое страницы');
$page->tags(); // Tag::tags $page->authors(); // Author::authors()