Определение подпространств имен
(PHP 5 >= 5.3.0)
Так же как файлы и каталоги, пространства имен PHP позволяют создавать иерархию имен. Таким образом, имя пространства может быть определено с подуровнями:
Пример #1 Определение пространства имен с иерархией
<?php
namespace MyProject\Sub\Level;
const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */ }
?>
- Обзор пространств имен
- Определение пространств имен
- Определение подпространств имен
- Описание нескольких пространств имен в одном файле
- Использование пространства имен: основы
- Пространства имен и динамические особенности языка
- Ключевое слово namespace и константа __NAMESPACE__
- Использование пространств имен: импорт/создание псевдонима имени
- Глобальное пространство
- Использование пространств имен: переход к глобальной функции/константе
- Правила разрешения имен
- Часто задаваемые вопросы (FAQ): вещи, которые вам необходимо знать о пространствах имен
Коментарии
I have read much about namespaces i found it very useful. But there was a time when i try to learn php namespaces i became irritate because i was unable to understand the. I know there are still many peoples out there who can't understand it. For those i have found a simple way to understand namespaces in php
Example:
You have a dog name "abcd" and after some day you bought new dog but unfortunately the new dog have same name as your first dog. Now whenever you will call "abcd" both of the dogs will come but you don't want both of them. To solve this problem we use namespaces in
Coding Example:
If you want to create two functions, Constants, Classes on the same file you will face an error because you cannot create two same name functions, classes, constants in same file in any programming language.
Example in Code:
Problem
----------
<?php
function abc(){
return "Hello World";
}
function abc(){
return "Hello World";
}
abcd(); // Error because we cannot use same name for functions
?>
Solution
----------
<?php
namespace first_function;
function abc(){
return "Hello World";
}
namespace second_function;
function abc(){
return "Hello World";
}
/*Now to call any of these functions first you will have to specify the namespace of target function like. Note this example is to call functions only*/
function second_exampleabc();
?>
now one problem here is how would we difference between functions, classes and constants
for that whenever we will call function we will have to tell the interpreter that we are calling functions from given namespace as i already have called
Step 1.
Tell the interpreter the type of target.
Step 2.
Type the namespace you have use.
Step 3.
Call your target functions, class or constant.