The namespace definition method of the PHP namespace is described in detail.

Source: Internet
Author: User

The namespace definition method of the PHP namespace is described in detail.

This document describes how to define the namespace of a PHP namespace. We will share this with you for your reference. The details are as follows:

Define a namespace

I don't want to explain the name of a space here. A better explanation is to prove it using an example:

For example:

The following code is a file in "test. php:

Namespace Test; class Test {public function Ttest () {echo "this is the Test method in Test". "<br> ";}}

Next, I will use three different access methods. I will write these three access programs in a file named "index. php:

Method 1:

namespace Index;require 'test.php';$T=new \Test\Test();$T->Ttest();

The result is:

This is the Test method in Test.

Method 2:

namespace Index;namespace Test;require 'test.php';$T=new Test();$T->Ttest();

The result is:

This is the Test method in Test.

Method 3:

namespace Index;require 'test.php';use Test\Test;$T=new Test();$T->Ttest();

The result is:

This is the Test method in Test.

Note: namespace Index can be written or not written. This is only the name of the index. php file space. The results of these three methods are the same.

Define a sub-namespace

Definition:

It has a similar relationship with directories and files. The PHP namespace also allows you to specify a hierarchical namespace name. Therefore, namespace names can be defined in different layers.

For example, this is my custom project directory:

One. php

namespace projectOne\one;class Test{    public function test(){     return "this is a test program";    }}

To access the Test () method under the test class in one. php, the code in Two is as follows:

Two. php

namespace projectOne\one;require '../projectOne/One.php';$O=new Test();echo $O->test();

Output: this is a test program

Define multiple namespaces in the same file and access each other

Test. php

namespace projectOne\one{    class test{      public function hello(){        return "helloworld";      }    }}namespace projectOne\Two{    class project{      public function world2(){        return "welcome to china";      }    }    class project2 extends \projectOne\one\test{      public function wo(){        return "this is my test function ,it is name wo";      }    }}namespace projectOne\Two{    $p=new project2();    echo $p->wo()."<br>";    echo $p->hello();}

Output: this is my test function, it is name wo
Helloworld

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.