Home  >  Article  >  Backend Development  >  How to define namespace in PHP

How to define namespace in PHP

墨辰丷
墨辰丷Original
2018-05-24 11:38:441336browse

This article mainly introduces the definition method of PHP namespace namespace, and analyzes in detail the definition method and related precautions of PHP namespace namespace and sub-namespace in the form of examples. Friends in need can refer to it

The example in this article describes the definition method of PHP namespace namespace. Share it with everyone for your reference. The details are as follows:

Define the namespace

For the naming of the space, I don’t want to explain it in words here. A better explanation is to use Examples to prove:

For example:

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

namespace Test;
class Test{
    public function Ttest(){
     echo "这是Test里面的测试方法"."<br>";
    }
}

Next I will use three different ways to access. I wrote these three access programs in a file named "index.php":

Method 1 :

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

The result is:

This is the test method in Test

Method 2:

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

The result is:

This is the test method in Test

Method three:

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

The result is:

This is the test method in Test

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

Define sub-namespace

Definition:

Much like the relationship between directories and files, PHP namespaces also allow you to specify hierarchical namespaces The name. Therefore, namespace names can be defined in a hierarchical manner.

The example is as shown below, this is my customized project directory:

one.php

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

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

Two.php

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

Output: this is a test program

Multiple namespaces are defined in the same file and they 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

The above is the entire content of this article, I hope it will be helpful to everyone learning helps.


Related recommendations:

ThinkPHP Function details: D method

AboutPHPRelevant knowledge points on realizing user login (2)

AboutPHPRelevant knowledge points on realizing user login (1) )

PHPCommon methods to get file extensions

##

The above is the detailed content of How to define namespace in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn