Namespace in php, namespace

Source: Internet
Author: User

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

 

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.