What does PHP namespace mean? I read the Help file. Don't let me read it.

Source: Internet
Author: User
What does {code...} mean? In my experiment, I understand that a non-qualified name means that no prefix is added when calling functionclass $, that is, the class or $ or function in the file where the code is located. Is that correct?
A non-qualified name or a class name that does not contain a prefix, for example, $ a = new foo (); or foo: staticmethod ();. If the current namespace is currentnamespace, foo will be parsed as currentnamespace \ foo. If the code using foo is global and is not included in any namespace, foo will be parsed as foo. Warning If the function or constant in the namespace is not defined, the undefined function name or constant name will be resolved to the global function name or constant name.

What does this mean? My experiment comprehension:
Unrestricted name
That is, when calling function class $, no prefix is added,

This is to call the class, $, or function in the file where the code is located. Is that correct?

Reply content:
A non-qualified name or a class name that does not contain a prefix, for example, $ a = new foo (); or foo: staticmethod ();. If the current namespace is currentnamespace, foo will be parsed as currentnamespace \ foo. If the code using foo is global and is not included in any namespace, foo will be parsed as foo. Warning If the function or constant in the namespace is not defined, the undefined function name or constant name will be resolved to the global function name or constant name.

What does this mean? My experiment comprehension:
Unrestricted name
That is, when calling function class $, no prefix is added,

This is to call the class, $, or function in the file where the code is located. Is that correct?

Check the source code.

Namespace Project \ Model; // This file belongs to the namespace Project \ Model. If no special description is provided, the classes that will appear later will be the classes in this space, for example, User indicates \ Project \ Model \ User, Table \ User indicates \ Project \ Model \ Table \ Useruse Zend \ Table; // calls the \ Zend \ Table class, when you use this declaration and then call $ table = new Table ();, this Table refers to \ Zend \ Tableuse Project \ Model \ Table PTable; // call the \ Project \ Model \ Table class and specify the alias PTable. When $ table = new PTable (); is called, this PTable indicates that the Project \ Model \ Table // is declared in the above Code, the absolute path class User extends \ Project \ Model {// User is not specified, so the full name of this class is \ Project \ Model \ User, \ Project \ Model refers to the class \ Project \ Model (cannot be written as extends Model; otherwise it is considered as \ Project \ Model) because it starts with a backslash )} class \ User extends Table \ User {// starts with a backslash. Therefore, the full name of this class is \ User, and the User following this class does not have a slash, \ Project \ Model \ Table \ User}

It can be seen that for namespace and use statements, the class uses the full name, and must start with a backslash to indicate the full name of the class elsewhere, otherwise, the full name is a backslash + the current namespace + class name.

In layman's terms: namespace is the location information of a file (globally unique)
For example

 lib/pay/weixin.php lib/order/weixin.php

The space of the two weixin. php instances is different.
The namespace is used to solve the problem of duplicate class names.
You don't need to do the experiment. It will make you dizzy.

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.