The namespace of PHP

Source: Internet
Author: User
PHP's namespace (namespace) is only available after php5.3. This concept is already very early in C #, and the namespace in PHP is actually the same as the C # concept.

Why should I use namespace in PHP?

Assuming that you do not use namespace, the name of each class in a project must be fixed. Because PHP has a file that corresponds to a class name, whether it's calling AutoLoad or calling a loaded class at new. So when there is no namespace, we think of various naming conventions to distinguish different classes, such as Project1_school1_class1_student or project2_school_class_student.

The introduction of namespace can be effectively circumvented, a namespace equivalent to a file path, when looking for this class, it will go to the corresponding file path to find the class definition file.

Defined

!--? php namespace Myproject;   

Use

!--? php use Myproject/school;    

Alias

!--? php use Myproject/school as School1; The alias  

Namespace is parsed at run time. Use is equivalent to a declaration that does not parse and load. For example:

test.php

!--? phpuse my\name;require_once ("/home/yejianfeng/handcode/  Test/namespace1.php "); $a = new my\name\a (); $a--->print1 (); 

namespace1.php

!--? phpnamespace my\name;class A {public Function Print1 () {        Echo 11; }}  
  • 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.