Introduction to the namespace of PHP

Source: Internet
Author: User
This article mainly introduces the PHP namespace, has a certain reference value, now share to everyone, the need for friends can refer to

What is a PHP namespace?

(PHP 5 >= 5.3.0, PHP 7)

What is a namespace? In a broad sense, namespaces are a way of encapsulating things. This abstract concept can be seen in many places. For example, in an operating system, a directory is used to group related files, and for files in a directory, it plays the role of a namespace. For example, file foo.txt can exist in both directory /home/greg and /home/other , but there cannot be two foo.txt files in the same directory. In addition, when accessing the foo.txt file outside of directory /home/greg , we must place the directory name and directory delimiter before the file name to get /home/greg/foo.txt. The application of this principle to the field of programming is the concept of namespaces.

In PHP, namespaces are used to solve two types of problems encountered when writing a class library or application to create reusable code such as classes or functions:

    1. User-written code conflicts with the name of a class/function/constant or third-party class/function/constant inside PHP.

    2. Creates an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem), improving the readability of the source code.

Although any valid PHP code can be included in a namespace, only the following types of code are affected by namespaces: classes (including abstract classes and traits), interfaces, functions, and constants.

The namespace is declared by the keyword namespace . If a file contains a namespace, it must declare the namespace before all other code, except one: the DECLARE keyword.

The only valid code before declaring the namespace is the declare statement that defines how the source file is encoded. In addition, all non-PHP code, including whitespace characters, cannot appear before the declaration of the namespace:


In addition, unlike other language features in PHP, the same namespace can be defined in multiple files, allowing the content of the same namespace to be split into separate files.

1. Example One
First we set up two classes of files

a.php

<?phpclass test{public    function Ceshi () {        echo __file__;    }}

b.php

<?phpclass test{public    function Ceshi () {        echo __file__;    }}

index.php

<?phprequire_once ("a.php"); Require_once ("b.php");

Run the index.php file now

You will find that there is a fatal error: Fatal Error:cannot redeclare class Test in ... Obviously, you cannot re-declare the test class because you introduced two times, and the class names in the two files are the same, conflicting. At this point the namespace is needed to solve the problem, and it's easy.

2. Example II
We will now modify the two class files slightly.

a.php

<?phpnamespace demo1\a\test;class test{public    function Ceshi () {        echo __file__;    }}

b.php

<?phpnamespace demo1\b\test;class test{public    function Ceshi () {        echo __file__;    }}

The namespace keyword is used to declare namespaces. Now run index.php find no error, modify index.php to make method call test

index.php

<?phprequire_once ("a.php"); Require_once ("b.php"); $a 1 = new demo1\a\test\test (); $a 1->ceshi ();

Run the index.php file now

D:\phpStudy\WWW\demo\demo1\a.php

3. example Three

Now there is another situation, such as I need to instantiate a.php in the test class multiple times, so every time we need to complete the namespace information is more difficult to do? Like what:

<?phprequire_once ("a.php"); Require_once ("b.php"); $a 1 = new demo1\a\test\test (); $a 2 = new demo1\a\test\test (); $a 1- >ceshi (); Echo ' <br/> '; $a 2->ceshi ();

Although there is no error, but you will find it troublesome, every time you need to fully write the name of the namespace, although not error and can be ctrl+c,ctrl+v, but not very beautiful (^_^).

You can do this.

index.php

<?phprequire_once ("a.php"), require_once ("b.php"), use demo1\a\test\test, $a 1 = new test (), $a 2 = new test (); $a 1-> Ceshi (); Echo ' <br/> '; $a 2->ceshi ();

The use keyword is used to introduce a class, using a namespace to represent a class. You can instantiate the operation directly later

4. example Five

Then another question came up, as follows:

index.php

<?phprequire_once ("a.php"); Require_once ("b.php"); use demo1\a\test\test;use demo1\b\test\test; $a = new Test (); $b = New Test (); $a->ceshi (); Echo ' <br/> '; $b->ceshi ();

Run the index.php file now

Fatal error:cannot use demo1\b\test\test as Test because the name was already in use in D:\phpStudy\WWW\demo\demo1\in dex.php on line 5

because the namespace is used, but the two classes have the same name, it is test, the program does not know that the second Test class is the test class in b.php, and you use the AS keyword

index.php

<?phprequire_once ("a.php"); Require_once ("b.php"); use Demo1\a\test\test;use demo1\b\test\test as bTest; $a = new Test (); $b = new Btest (); $a->ceshi (); Echo ' <br/> '; $b->ceshi ();

The As keyword defines an alias for the class name, which effectively prevents the same class name from conflicting

5. Example VI

For example, we build a global class file at the a.php level: c.php:

c.php

<?phpclass test{public    function Ceshi () {        echo __file__;    }}

You can do this in the index.php file to call the test method in c.php

<?phprequire_once ("a.php"); Require_once ("b.php"); Require_once ("c.php"); use Demo1\a\test\test;use demo1\b\Test  \test as btest; $a = new Test (); $a->ceshi (); Echo ' <br/> '; $b = new Btest (); $b->ceshi (); Echo ' <br/> '; $c = New \test (); $c->ceshi (); Echo ' <br/> ';

We call this Class A global class, and if you want to use a backslash "\" before the class

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.