pk10赛车计划软件百度云

Fuzhou website construction only needs 1,000 yuan to include domain name space, details 18558752805

  • PHP namespace, require, use differences

Fuzhou Website Construction > Site News > PHP Technology >

Suppose there is file a.php code

Copy code
  class a{ // 类a public function afun() // 函数afun { <? php class a { // Class a public function afun () // Function afun { 
      " aaaa " ; echo " aaaa " ; 
  } 
  ?> } ?> 
Copy code

There is file b.php code

Copy code
  class a{ // 类a public function bfun() // 函数bfun { <? php class a { // Class a public function bfun () // Function bfun { 
      " aaaa " ; echo " aaaa " ; 
  } 
  ?> } ?> 
Copy code

A file c.php now has the following requirements

Requirement 1: You need to call afun () in c.php.

Requirement 1 is very simple. Everyone knows how to import a.php file and instantiate a object. Require_once ('a.php');

Requirement 2: call bfun () in c.php

The same as require_once ('b.php') above.

Requirement 3: Call afun () and bfun () at the same time in c.php

Here comes the question. If you require files a, php, and b.php at the same time, is class a a.php or b.php when you instantiate an object? (Which horny horns say that you will not replace class a in b.php with another name, please bypass!); So the namespace namespace was introduced to facilitate the management of classes.

OK, let's modify the file a.php b.php code to join the namespace !

a.php:

Copy code
  namespace ns\a; // 定义命名空间 class a{ // 类a public function afun() // 函数afun { echo " aaaa " ; <? php namespace ns \ a; // define namespace class a { // class a public function afun () // function afun {echo " aaaa " ; 
      } 
      ?> } ?> 
Copy code

b.php:

Copy code
  namespace ns\b; // 定义命名空间 class a{ // 类a public function bfun() // 函数bfun { <? php namespace ns \ b; // define namespace class a { // class a public function bfun () // function bfun { 
      " aaaa " ; echo " aaaa " ; 
  } 
  ?> } ?> 
Copy code

So in c.php we can reference how to define the namespace defined above? This is the keyword use comes out!

c.php: call afun, bfun at the same time in c.php

Copy code
  <? php 
  ' a.php ' ); // 引入文件a.php require_once( ' \b.php ' ); // 引入文件a.php use ns\a\a as a; // 使用命名空间ns\a中的类a 并用a替换 use ns\b\a as b; // 使用命名空间ns\b中的类a 并用b替换 $a= new a(); // 实例化a.php 中的类a $a-> afun(); require_once ( ' a.php ' ); // Introduce file a.php require_once ( ' \ b.php ' ); // Introduce file a.php use ns \ a \ a as a; // Use namespace ns \ a Class a in and replace use a with ns \ b \ a as b; // use class a in namespace ns \ b and replace with b $ a = new a (); // instantiate class a in a.php $ a-> afun (); 
  = new b(); // 例化b.php 中的类a $a->bfun(); $ a = new b (); // Instantiate class a in b.php $ a-> bfun (); 
Copy code

The above example should be enough to illustrate the relationship between namespace and use

To summarize:

It can be understood that the namespace namespace is a mechanism introduced to facilitate the management of classes. We can put classes with similar functions under the same namespace and directly introduce the space when used. Simply put, the namespace is the Make a package. For example, the following code:

Copy code
   my\test; class  a namespace my \ test; class a 
  public  function test() { public function test () 
        { 
            " aaaa " ; echo " aaaa " ; 
        } 
  class  c } class c 
  public  function test() { public function test () 
      { 
          " aaaa " ; echo " aaaa " ; 
      } 
  } 
Copy code

We can group classes a and c together through namespaces. Of course, class a and class c can be defined in different files, as long as their namespace is the same, they will be grouped together. Under this mechanism, we will find that the future organization will be very clear! After we "package" a class through a namespace, we can call it by simply introducing the "package" through use! If our project has many files and thousands of classes, then it is very important for class management. This is why many PHP frameworks use namespaces. Of course, you do n’t need to use small projects to write your own!

Note :

The keyword namespace is followed by the name and not the path! We see that many framework namespaces are followed by "path" in order to let us know the file location of the namespace. When we have a lot of files, such names will be convenient for us to find.

The keyword use is usually followed by a class name such as:

use a \ b // means use class b in the namespace

use a \ b as c // Indicates the use of class b in the namespace and assigns an extra label c. You can use c but class b.

In fact, use is not required! As in the above example file a.php we use class a;

use ns \ a as a;

$ a = new a ();

Equivalent to:

$ a = new ns \ a \ a ();

And namespace use is completely different from require_once (include is similar)! Many people think that use is to include the file. This is the wrong view! Some people even think that use does not require require, and such a view can only show that you do not understand the purpose of namespaces at all! Imagine we use use. If we don't even know where the namespace is, how do we use classes? So if the namespace must use require (include) in other files, first include the file!

Scan QR code to share on WeChat
confirm

Copyright © 2014-2019 buxie520.com Fuzhou website production