A namespace is a set that means a model, an abstract repository, or an environment created for the logical grouping of unique identifiers (that is, names). An identifier defined in a namespace is associated with this space. The same identifier can be independently defined in several spaces. Thus, the value associated with an identifier defined in one namespace may (or may not) have the same (or rather, different) meaning, like the same identifier defined in another space. Languages ​​with namespace support define rules that indicate to which namespace the identifier belongs (that is, its definition). wiki
use
Warning: for some reason, php does not allow the use of the use keyword in blocks of conditions and cycles.
Attention: the namespase keyword must be located at the very beginning of the file immediately after <? php
<? php namespace A { class A { public static function say() { echo ' '; } } }
<? php namespace B { class A { public static function say() { echo ' B'; } } }
<? php namespace A; class A { public static function say() { echo ' '; } }
<? php require_once 'A.php'; require_once 'B.php'; use A\A; use B\A;
<? php require_once 'A.php'; require_once 'B.php'; use A\A; use B\A; A\A::say(); B\A::say();
Attention: the use of the scope resolution operator (: :) in php namespaces is not allowed ! The only thing for which it is suitable is to refer to static class methods and constants. At first they wanted to use it for the namespace, but then they refused because of the problems that arose. Therefore, the construction of the form A :: A :: say (); is invalid and will result in an error.
Attention: in order to avoid misunderstanding, it is necessary to escape this symbol when using it in the lines: '\\'
<? php namespace A { class A { public static function say() { echo ' '; } } } namespace A\subA { class A { public static function say() { echo ' '; } } }
<? php require_once 'A.php'; require_once 'B.php'; use A\A as A; use B\A as B; use A\subA as sub A::say(); A::say(); sub::say();
Attention: In order for your classes to load the class name must match the file name!
<?php namespace yourNameSpace { class Autoloader { const debug = 1; public function __construct(){} public static function autoload($file) { $file = str_replace('\\', '/', $file); $path = $_SERVER['DOCUMENT_ROOT'] . '/classes'; $filepath = $_SERVER['DOCUMENT_ROOT'] . '/classes/' . $file . '.php'; if (file_exists($filepath)) { if(Autoloader::debug) Autoloader::StPutFile((' ' .$filepath)); require_once($filepath); } else { $flag = true; if(Autoloader::debug) Autoloader::StPutFile((' ')); Autoloader::recursive_autoload($file, $path, &$flag); } } public static function recursive_autoload($file, $path, $flag) { if (FALSE !== ($handle = opendir($path)) && $flag) { while (FAlSE !== ($dir = readdir($handle)) && $flag) { if (strpos($dir, '.') === FALSE) { $path2 = $path .'/' . $dir; $filepath = $path2 . '/' . $file . '.php'; if(Autoloader::debug) Autoloader::StPutFile((' <b>' .$file .'</b> in ' .$filepath)); if (file_exists($filepath)) { if(Autoloader::debug) Autoloader::StPutFile((' ' .$filepath )); $flag = FALSE; require_once($filepath); break; } Autoloader::recursive_autoload($file, $path2, &$flag); } } closedir($handle); } } private static function StPutFile($data) { $dir = $_SERVER['DOCUMENT_ROOT'] .'/Log/Log.html'; $file = fopen($dir, 'a'); flock($file, LOCK_EX); fwrite($file, ('â•‘' .$data .'=>' .date('dmY H:i:s') .'<br/>â•‘<br/>' .PHP_EOL)); flock($file, LOCK_UN); fclose ($file); } } \spl_autoload_register('yourNameSpace\Autoloader::autoload'); }
<? php require_once 'Autoloader.php'; use Autoloader as Autoloader; use A\A as A; use B\A as B; use A\subA as sub A::say(); A::say(); sub::say();
<? php namespace mySpace { class test { __construct() { //; } function sayName($name) { echo ' ' . $name; } static function sayOther() { echo ' '; } } }
<? php require_once 'Autoloader.php'; use Autoloader as Autoloader; use mySpace\test as test //, $class = 'test'; // $obj = new $class; $obj->sayName('test'); // test\sayName('test2'); // $obj::sayName('test'); // test::sayName('test2');
Source: https://habr.com/ru/post/132736/
All Articles