this article illustrates the definition of the PHP namespace namespace and the usages of importing use. Share for you for your reference, specifically as follows:

in PHP, the appearance of the same name function or the same class is not allowed. For prevent programmers from defining the class name or function name in the project, there is a repeated conflict , and the concept of namespaces is introduced in PHP5.3.

1. namespaces, the code is divided into different spaces, the class names of different spaces are independent and do not conflict with each other. there can be multiple namespaces in a PHP file, and no code is available before the first namespace. The content space after the declaration code will belong to this namespace , for example:

 < PHP echo 111; / /? Because the code namespace and the error of namespace Teacher; class Person{function __construct (echo'Please) {study}}! '; 

< strong>2. call different space classes or methods need to specify the namespace. For example:

 < PHP namespace Teacher; class? Person{function __construct (echo'Please) {study! < br/> 'function (Person);}} {return'You must stay here!';}; namespace Student; class Person{function __construct ('I want) {echo to < play! Br/>}} '; new (Person); / / the space (Student space) new (TeacherPerson); //Teacher new (StudentPerson) //Student space; echo space TeacherPerson (//Teacher); space Person function output: 

 I 

want to play Please I want to! Study! Play! You must stay here!

3. into other files are not belong to the namespace namespace, which belongs to the public space or file defines the namespace itself. example:

first define a 1.php and 2.php file:

 < PHP //1.php class Person{function? __construct (echo'I AM) {one! < br/>}} '; 
 < PHP namespace Newer; require_once?'./1.php'; new (Person); / / error, no Person; new (Person); / / I am 

output tow!

; < PHP //2.php namespace Two class? Person{function __construct (echo'I AM) {tow! < br/>}} '; 
 < PHP namespace New; require_once?'./2.php'; new (Person); / / error (the space), can not find the Person (Person); new; / / error, (public space) to find Person; new TwoPerson (I am tow); / / output; using the method of 

4.! We see in use: (use Refer to

 namespace) can be abbreviated SchoolParents; class Man{function __construct (echo'Listen to) {teachers! < br/>}} '; namespace SchoolTeacher; class Person{function (__construct) {echo'Please study! < br/>}}'; namespace SchoolStudent; class Person{function (__construct) {echo'I want to play! < br/>}} '; new (Person); / / I want to play new output (SchoolTeacherPerson!); / / Please new TeacherPerson output study! (); / / error - use SchoolTeacher; new TeacherPerson (Please); / / use SchoolTeacher output study! - as (TcPerson Tc; new); / / Please - use output study! SchoolTeacherPerson; new (Person); / / error - use (Man SchoolParentMan; new ); / / error 

PHP more about the related content of interested readers can see the special station: "php object-oriented programming tutorial" , PHP " basic grammar tutorial "PHP operation and operator Usage Summary ", "PHP network programming skills summary " and "PHP array (Array) technique," php string (string) " php+mysql "and" database operation tutorial "php " common database operation skills summary

hopes the PHP program designed to help to everyone.

you might be interested in this article:


This concludes the body part

This paper fixed link:http://www.script-home.com/php-namespace-namespace-definition-and-import-use-usage-detailed-explanation.html | Script Home | +Copy Link

Article reprint please specify:PHP namespace namespace definition and import use usage detailed explanation | Script Home

You may also be interested in these articles!