mirror of
https://github.com/igorsimdyanov/php8.git
synced 2025-08-01 16:49:53 +00:00
Глава 36: пространство имен
This commit is contained in:
18
namespace/PHP8/page.php
Normal file
18
namespace/PHP8/page.php
Normal 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
25
namespace/PHP8/seo.php
Normal 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
11
namespace/PHP8/tag.php
Normal 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
16
namespace/absolute.php
Normal 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
8
namespace/anonim.php
Normal 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
8
namespace/autoload.php
Normal 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
22
namespace/namespace.php
Normal 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;
|
||||
}
|
||||
}
|
4
namespace/namespace_const.php
Normal file
4
namespace/namespace_const.php
Normal file
@ -0,0 +1,4 @@
|
||||
<?php
|
||||
namespace PHP8;
|
||||
|
||||
echo __NAMESPACE__; // PHP8
|
32
namespace/namespace_fail.php
Normal file
32
namespace/namespace_fail.php
Normal 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>
|
12
namespace/namespace_key.php
Normal file
12
namespace/namespace_key.php
Normal 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 />';
|
6
namespace/namespace_use.php
Normal file
6
namespace/namespace_use.php
Normal 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
26
namespace/namespaces.php
Normal 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;
|
||||
}
|
||||
}
|
29
namespace/namespaces_alt.php
Normal file
29
namespace/namespaces_alt.php
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
6
namespace/namespaces_alt_use.php
Normal file
6
namespace/namespaces_alt_use.php
Normal 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
6
namespace/relative.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
namespace PHP8;
|
||||
|
||||
require_once 'namespaces.php';
|
||||
$page = new classes\Page('Контакты', 'Содержимое страницы');
|
||||
functions\debug($page);
|
5
namespace/spl_autoload_register.php
Normal file
5
namespace/spl_autoload_register.php
Normal file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
spl_autoload_register();
|
||||
|
||||
$page = new PHP8\Page('О нас', 'Содержимое страницы');
|
||||
$page->tags(); // Tag::tags
|
10
namespace/use.php
Normal file
10
namespace/use.php
Normal 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
7
namespace/wrong.php
Normal 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
|
Reference in New Issue
Block a user