Namespace in php, namespace
Description:
Namespace (namespace), which is supported by php5.3. There are two main functions: 1. Avoid getting too long a class name. 2. When multiple frameworks are used together, classes with the same name do not conflict.
Namespace. You can see it by name. The purpose is to name it. Otherwise, it cannot be called QQ space. There seems to be a strange addition.
The following is the sample code:
1 // There is a class named Person in test1.php, which is put in a namespace named shop. 2 <? Php 3 namespace shop; // This statement should be placed at the top of the php file. Even the header must give way. 4 5 header ('content-type: text/html; charset = UTF-8 '); 6 7 class Person {8 public $ name = 'leonard'; 9} 10?> 11 // In test2.php at the same level, there is also a class named Person, which is placed in a namespace named admin 12 <? Php13 14 namespace admin; // The above can contain blank lines. None of the above can be used for 15 16 Export de_once ('./test. php'); 17 18 use shop as s; // The shop namespace is referenced here. To use classes in test1.php, you must write this line. As is short for the namespace name. 19 20 class Person {21 public $ name = 'sheldon '; 22} 23 24 $ p1 = new s \ Person (); // The as abbreviation is used above, s refers to the namespace shop. If you do not use as, you can use new shop \ Person () to instantiate an object. 25 echo $ p1-> name; // Leonard26 27 $ p2 = new Person (); // if you do not specify a namespace, find the Person class in your namespace, if it cannot be found, an error is reported. 28 echo $ p2-> name; // Sheldon