Phpnamespace How to use

Source: Internet
Author: User
This time to everyone to bring phpnamespace how to use, the use of phpnamespace Note what, the following is the actual case, together to see.

The examples in this paper describe the PHP namespace namespace definition and import use usage. Share to everyone for your reference, as follows:

In PHP, a function with the same name or a class with the same name is not allowed. The concept of namespaces is introduced in PHP5.3 to prevent duplicate conflicts in the class names or function names that programmers define in the project.

1. Namespaces, the code is divided into different space, the class names of different spaces are independent of each other, non-conflicting. There can be more than one namespace in a php file and no code in front of the first namespace. The code after the content space declaration belongs to this namespace , for example:

<?phpecho 111;  Due to namespace before the code and error namespace Teacher;class person{  function construct () {   echo ' please study! ';  }

2. Calling a class or method in a different space requires specifying a namespace. For example:

<?phpnamespace teacher;class person{  function construct () {   echo ' please study!<br/> ';  }} function person () {  return ' You must stay here! ';}; namespace Student;class person{  function construct () {   echo ' I want to play!<br/> ';  }} New person ();     This space (student space) new \teacher\person ();   Teacher Space New \student\person ();   Student Space Echo \teacher\person ();   person function under teacher space

Output:

I want to play! Please study! I want to play! You must stay here!

3. The introduction of other files within a namespace does not belong to this namespace, but to public spaces or namespaces defined in the file itself. Example:

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

<?php  //1.phpclass person{function construct () {   echo ' I am one!<br/> ';  }}
<?phpnamespace newer;require_once './1.php '; new person ();  Error, can not find person;new \person ();  Output I am tow!;
<?php  //2.phpnamespace twoclass person{function construct () {   echo ' I am tow!<br/> ';  }}
<?phpnamespace new;require_once './2.php '; New person ();  Error, (current space) can not find person;new \person ();  Error, (public space) can not find person;new \two\person (); Output I am tow!;

4. Let's take a look at how use is used: (using alater reference can be abbreviated)

namespace School\parents;class man{  function construct () {   echo ' Listen to teachers!<br/> ';  }} namespace School\teacher;class person{  function construct () {   echo ' please study!<br/> ';  }} namespace School\student;class person{  function construct () {   echo ' I want to play!<br/> ';  }} New person ();     Output I want to play!new \school\teacher\person (); Output study!new Teacher\person ();   Error----------use School\teacher;new Teacher\person ();   Output study!----------use School\teacher as Tc;new Tc\person ();   Output study!----------use \school\teacher\person;new person ();   Error----------use \school\parent\man;new man ();   Error

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP Direct implementation to generate poster ads

PHP to determine whether to open or open the browser

How to query a different database with an SQL statement

JS gets the value within the first element in the Select drop-down box

Related Article

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.