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

Source: Internet
Author: User
Keywords Php
非限定名称,或不包含前缀的类名称,例如 $a=new foo(); 或 foo::staticmethod();。如果当前命名空间是 currentnamespace,foo 将被解析为 currentnamespace\foo。如果使用 foo 的代码是全局的,不包含在任何命名空间中的代码,则 foo 会被解析为foo。 警告:如果命名空间中的函数或常量未定义,则该非限定的函数名称或常量名称会被解析为全局函数名称或常量名称。

What does that mean? I do the experiment to understand:
Unqualified name
is to call the function class $ without any prefix,

is the call to the code's own file in the class or $ or function, so understand it?

Reply content:

非限定名称,或不包含前缀的类名称,例如 $a=new foo(); 或 foo::staticmethod();。如果当前命名空间是 currentnamespace,foo 将被解析为 currentnamespace\foo。如果使用 foo 的代码是全局的,不包含在任何命名空间中的代码,则 foo 会被解析为foo。 警告:如果命名空间中的函数或常量未定义,则该非限定的函数名称或常量名称会被解析为全局函数名称或常量名称。

What does that mean? I do the experiment to understand:
Unqualified name
is to call the function class $ without any prefix,

is the call to the code's own file in the class or $ or function, so understand it?

Look at the source.

namespace Project\Model; //本文件隶属命名空间Project\Model,如果没有特别说明,以后出现的类名称为该空间下的类,如 User表示\Project\Model\User,Table\User表示\Project\Model\Table\Useruse Zend\Table; //调用\Zend\Table这个类,使用该声明之后再调用$table = new Table();时,这个Table是指\Zend\Tableuse Project\Model\Table PTable; //调用\Project\Model\Table这个类,并指定别名PTable,当调用$table = new PTable();时,这个PTable是指Project\Model\Table//在以上代码中属于声明,不需要特别使用\开头说明是绝对路径class User extends \Project\Model { //User无特别声明,所以该类全名为\Project\Model\User,而\Project\Model因为是反斜杠开头,指\Project\Model这个类(不能写成extends Model,否则会被认为是\Project\Model\Model)}class \User extends Table\User { //使用了反斜杠开头,因此该类全名就是\User,后面的User没有斜杠,则是指\Project\Model\Table\User}

Thus, for namespace and use statements, where the class uses the full name, and in other places the full name of the class must be preceded by a backslash, otherwise its full name is a backslash + the current namespace + class name

Say the popular point: The namespace is the location information of a file (globally unique)
Like what

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

These two weixin.php have different spaces.
Namespaces are meant to solve the problem of repeating class names.
You don't have to do the experiment, you're going to get dizzy.

  • Related Article

    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.