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.

Definition and use of namespace

Defined:

123 <?phpnamespaceMyproject;

Or

12345 <?phpnamespace Myproject {}

Use:

123 <?phpuseMyproject/School;

123 <?phpuse Myproject/School asSchool1;   // 别名

Namespaces are parsed at run time. Use is equivalent to a declaration that does not parse and load. For example, the following:

test.php

12345 <?phpusemy\name;require_once("/home/yejianfeng/handcode/test/namespace1.php");$a = newmy\name\A();$a->Print1();

namespace1.php

1234567 <?php namespace  my\name; class  a {           public   function  print1 () {                   echo  11; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; } }

Although the require_once is under use, it can be run normally, because the program only loads the namespace when new My\name\a () is loaded my\name

Global classes and Namespace classes

If you want to new a global class using new \a ()

If you want to new a namespace class, use new My\namespace\a ()

The Order of namespaces

The most error-prone thing to do since you have a namespace is what the search path for this class is when you use the class.

If we can figure out this example in manual, we can all find out the order.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 <?phpnamespaceA;useB\D, C\E asF;// 函数调用 foo();      // 首先尝试调用定义在命名空间"A"中的函数foo()            // 再尝试调用全局函数 "foo"\foo();     // 调用全局空间函数 "foo" my\foo();   // 调用定义在命名空间"A\my"中函数 "foo" F();        // 首先尝试调用定义在命名空间"A"中的函数 "F"             // 再尝试调用全局函数 "F"// 类引用newB();    // 创建命名空间 "A" 中定义的类 "B" 的一个对象            // 如果未找到,则尝试自动装载类 "A\B"newD();    // 使用导入规则,创建命名空间 "B" 中定义的类 "D" 的一个对象            // 如果未找到,则尝试自动装载类 "B\D" newF();    // 使用导入规则,创建命名空间 "C" 中定义的类 "E" 的一个对象            // 如果未找到,则尝试自动装载类 "C\E"new\B();   // 创建定义在全局空间中的类 "B" 的一个对象            // 如果未发现,则尝试自动装载类 "B"new\D();   // 创建定义在全局空间中的类 "D" 的一个对象            // 如果未发现,则尝试自动装载类 "D"new\F();   // 创建定义在全局空间中的类 "F" 的一个对象            // 如果未发现,则尝试自动装载类 "F"// 调用另一个命名空间中的静态方法或命名空间函数B\foo();    // 调用命名空间 "A\B" 中函数 "foo" B::foo();   // 调用命名空间 "A" 中定义的类 "B" 的 "foo" 方法            // 如果未找到类 "A\B" ,则尝试自动装载类 "A\B"D::foo();   // 使用导入规则,调用命名空间 "B" 中定义的类 "D" 的 "foo" 方法            // 如果类 "B\D" 未找到,则尝试自动装载类 "B\D"\B\foo();   // 调用命名空间 "B" 中的函数 "foo" \B::foo();  // 调用全局空间中的类 "B" 的 "foo" 方法            // 如果类 "B" 未找到,则尝试自动装载类 "B"// 当前命名空间中的静态方法或函数A\B::foo();   // 调用命名空间 "A\A" 中定义的类 "B" 的 "foo" 方法              // 如果类 "A\A\B" 未找到,则尝试自动装载类 "A\A\B"\A\B::foo();  // 调用命名空间 "A\B" 中定义的类 "B" 的 "foo" 方法              // 如果类 "A\B" 未找到,则尝试自动装载类 "A\B"?>

The namespace of PHP

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.