Hướng dẫn using namespace in php

[PHP 5 >= 5.3.0, PHP 7, PHP 8]

Before discussing the use of namespaces, it is important to understand how PHP knows which namespaced element your code is requesting. A simple analogy can be made between PHP namespaces and a filesystem. There are three ways to access a file in a file system:

  1. Relative file name like foo.txt. This resolves to currentdirectory/foo.txt where currentdirectory is the directory currently occupied. So if the current directory is /home/foo, the name resolves to /home/foo/foo.txt.
  2. Relative path name like subdirectory/foo.txt. This resolves to currentdirectory/subdirectory/foo.txt.
  3. Absolute path name like /main/foo.txt. This resolves to /main/foo.txt.

The same principle can be applied to namespaced elements in PHP. For example, a class name can be referred to in three ways:

  1. Unqualified name, or an unprefixed class name like $a = new foo[]; or foo::staticmethod[];. If the current namespace is currentnamespace, this resolves to currentnamespace\foo. If the code is global, non-namespaced code, this resolves to foo. One caveat: unqualified names for functions and constants will resolve to global functions and constants if the namespaced function or constant is not defined. See Using namespaces: fallback to global function/constant for details.
  2. Qualified name, or a prefixed class name like $a = new subnamespace\foo[]; or subnamespace\foo::staticmethod[];. If the current namespace is currentnamespace, this resolves to currentnamespace\subnamespace\foo. If the code is global, non-namespaced code, this resolves to subnamespace\foo.
  3. Fully qualified name, or a prefixed name with global prefix operator like $a = new \currentnamespace\foo[]; or \currentnamespace\foo::staticmethod[];. This always resolves to the literal name specified in the code, currentnamespace\foo.

Here is an example of the three kinds of syntax in actual code:

file1.php

file2.php

Note that to access any global class, function or constant, a fully qualified name can be used, such as \strlen[] or \Exception or \INI_ALL.

Example #1 Accessing global classes, functions and constants from within a namespace

richard at richard-sumilang dot com

14 years ago

Syntax for extending classes in namespaces is still the same.

Lets call this Object.php:



And now lets create a class called String that extends object in String.php:



Now if you class String was defined in the same namespace as Object then you don't have to specify a full namespace path:



Lastly, you can also alias a namespace name to use a shorter name for the class you are extending incase your class is in seperate namespace:



- Richard Sumilang

Anonymous

7 years ago



Instead use fully qualified name for the exception to catch it

Lukas Z

10 years ago

Well variables inside namespaces do not override others since variables are never affected by namespace but always global:
"Although any valid PHP code can be contained within a namespace, only four types of code are affected by namespaces: classes, interfaces, functions and constants. "

Source: "Defining Namespaces"
//www.php.net/manual/en/language.namespaces.definition.php

tom at tomwardrop dot com

10 years ago

It seems the file system analogy only goes so far. One thing that's missing that would be very useful is relative navigation up the namespace chain, e.g.



That would be really nice, especially if you had really deep namespaces. It would save you having to type out the full namespace just to reference a resource one level up.

philip dot preisser at arcor dot de

11 years ago

Working with variables can overwrite equal variables in other namespaces

Chủ Đề