Глава 36: пространство имен

This commit is contained in:
Igor Simdyanov
2022-05-27 21:28:32 +03:00
parent bcef05e76b
commit 8b99b51ec8
18 changed files with 251 additions and 0 deletions

18
namespace/PHP8/page.php Normal file
View File

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

25
namespace/PHP8/seo.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace PHP8;
trait Seo
{
private $keyword;
private $description;
private $ogs;
public function keywords()
{
// $query = 'SELECT keywords FROM seo WHERE id = :id LIMIT 1'
echo 'Seo::keywords<br />';
}
public function description()
{
// $query = 'SELECT description FROM seo WHERE id = :id LIMIT 1'
echo 'Seo::description<br />';
}
public function ogs()
{
// $query = 'SELECT ogs FROM seo WHERE id = :id LIMIT 1'
echo 'Seo::ogs<br />';
}
}

11
namespace/PHP8/tag.php Normal file
View File

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

16
namespace/absolute.php Normal file
View File

@ -0,0 +1,16 @@
<?php
namespace PHP8;
function strlen($str)
{
return count(str_split($str));
}
// Или даже так
// function strlen($str) {
// return \strlen($str);
// }
// Это PHP8\strlen
echo strlen('Hello world!').'<br />';
// Это стандартная функция strlen()
echo \strlen('Hello world!').'<br />';

8
namespace/anonim.php Normal file
View File

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

8
namespace/autoload.php Normal file
View File

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

22
namespace/namespace.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace PHP8;
const VERSION = '1.0';
function debug(array|object $obj) : void
{
echo '<pre>';
print_r($obj);
echo '</pre>';
}
class Page
{
protected $title;
protected $content;
public function __construct(string $title = '', string $content = '')
{
$this->title = $title;
$this->content = $content;
}
}

View File

@ -0,0 +1,4 @@
<?php
namespace PHP8;
echo __NAMESPACE__; // PHP8

View File

@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="ru">
<head>
<title>Пространство имен</title>
<meta charset='utf-8' />
</head>
<body>
<?php
namespace PHP8;
const VERSION = '1.0';
function debug(array|object $obj) : void
{
echo '<pre>';
print_r($obj);
echo '</pre>';
}
class Page
{
protected $title;
protected $content;
public function __construct(string $title = '', string $content = '')
{
$this->title = $title;
$this->content = $content;
}
}
?>
</body>
</html>

View File

@ -0,0 +1,12 @@
<?php
namespace PHP8;
function strlen($str)
{
return count(str_split($str));
}
// Это PHP8\strlen
echo \PHP8\strlen('Hello, world!') . '<br />';
echo strlen('Hello, world!') . '<br />';
echo namespace\strlen('Hello, world!') . '<br />';

View File

@ -0,0 +1,6 @@
<?php
require_once 'namespace.php';
echo 'Версия ' . PHP8\VERSION . '<br />';
$page = new PHP8\Page('Контакты', 'Содержимое страницы');
PHP8\debug($page);

26
namespace/namespaces.php Normal file
View File

@ -0,0 +1,26 @@
<?php
namespace PHP8\constants;
const VERSION = '1.0';
namespace PHP8\functions;
function debug(array|object $obj) : void
{
echo '<pre>';
print_r($obj);
echo '</pre>';
}
namespace PHP8\classes;
class Page
{
protected $title;
protected $content;
public function __construct(string $title = '', string $content = '')
{
$this->title = $title;
$this->content = $content;
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace PHP8\constants
{
const VERSION = '1.0';
}
namespace PHP8\functions
{
function debug(array|object $obj) : void
{
echo '<pre>';
print_r($obj);
echo '</pre>';
}
}
namespace PHP8\classes
{
class Page
{
protected $title;
protected $content;
public function __construct(string $title = '', string $content = '')
{
$this->title = $title;
$this->content = $content;
}
}
}

View File

@ -0,0 +1,6 @@
<?php
require_once 'namespaces_alt.php';
echo 'Версия ' . PHP8\constants\VERSION . '<br />';
$page = new PHP8\classes\Page('Контакты', 'Содержимое страницы');
PHP8\functions\debug($page);

6
namespace/relative.php Normal file
View File

@ -0,0 +1,6 @@
<?php
namespace PHP8;
require_once 'namespaces.php';
$page = new classes\Page('Контакты', 'Содержимое страницы');
functions\debug($page);

View File

@ -0,0 +1,5 @@
<?php
spl_autoload_register();
$page = new PHP8\Page('О нас', 'Содержимое страницы');
$page->tags(); // Tag::tags

10
namespace/use.php Normal file
View File

@ -0,0 +1,10 @@
<?php
require_once 'namespaces_alt.php';
use PHP8\constants as constants;
use PHP8\functions as functions;
use PHP8\classes\Page as Page;
echo 'Версия ' . constants\VERSION . '<br />';
$page = new Page('Контакты', 'Содержимое страницы');
functions\debug($page);

7
namespace/wrong.php Normal file
View File

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