The namespace of PHP

Source: Internet
Author: User

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:

 //  fatal Error-The namespace must be the first statement of the program script ?>

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

<? PHP class test{    publicfunction  Ceshi () {        echo__file__;    }} 

b.php

<? PHP class test{    publicfunction  Ceshi () {        echo__file_ _;    }}

index.php

<? PHP require_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 a\test; class test{    publicfunction  Ceshi () {        echo__file__;    }} 

b.php

<? phpnamespace b\test; class test{    publicfunction  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

<? PHP require_once ("a.php"); require_once ("b.php"); $a New a\test\test (); $a->ceshi ();

Run the index.php file now

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

The namespace of PHP

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.