Home>Article>Backend Development> How to define namespace in PHP
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里面的测试方法"."
"; } }
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 '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 three:
namespace Index; require 'test.php'; 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 '../projectOne/One.php'; $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()."
"; 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:
ThinkPHPFunction 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!