Good PHP library without a good namespace

Source: Internet
Author: User
Tags aliases


Good PHP libraries are inseparable from a good namespace overview

Namespaces are a way of encapsulating things. This abstraction can be seen in many places, such as in the operating system, where directories are used to group related files, and directories play the role of namespaces for files in a directory. The application of this principle to the field of programming is the concept of namespaces. The version after PHP 5.3 begins to support namespaces.

Define namespaces

Namespaces are declared by keywords namespace . Any valid PHP code can be included in the namespace, but only the three types of classes, functions, and constants are affected by the namespace.

The only valid code before declaring the namespace is the statement that defines how the source file is encoded declare . The namespace must be the first statement of the program script, and all non-PHP code including whitespace characters cannot appear before the namespace declaration. The same namespace can be defined in multiple files.

Defining child namespaces

Like directories and files, PHP also allows you to specify a hierarchical namespace.

Define multiple namespaces in the same file

PHP supports this syntax, but it is not recommended. There are two syntactic forms for defining multiple namespaces in the same file: One is a simple list, and one is a syntax that uses curly braces. It is recommended to use the second type.


   
    declare(encoding = 'UTF-8');namespaceMyProject {/* MyProject */}namespace {/* global */}

The code in the global non-namespace is combined with the code in the namespace, and only the syntax in the form of braces is used. Nantong Long card Game Hall http://www.szjmswy.com/

Using Namespaces: Basics

To use namespaces, you must understand how PHP knows which namespace elements to use.

In the file system, there are three ways to access a file:

    • Relative file name Form
    • Relative path name Form
    • Absolute path name Form

Similar to the file system, in the PHP namespace, the class name can be referenced in three ways, requiring an understanding of the namespace name definition:

    • Unqualified name, class name that does not contain a prefix. The name does not contain an identifier for the namespace delimiter. For example Foo .
    • A qualified name that contains the name of the prefix with the identifier for the namespace delimiter in the name. For example Foo\Bar .
    • The fully qualified name that contains the name of the global prefix operator. The name contains the namespace delimiter, and an identifier that begins with a namespace delimiter. For example \Foo\Bar . namespace\Foois also a fully qualified name.

Namespaces and dynamic Language features

The implementation of a namespace is affected by the dynamic characteristics of the language itself, and sometimes the fully qualified name must be used.

namespaceKeywords and __NAMESPACE__ constants

The value of the constant __namespace__ is a string containing the name of the current namespace. In the global, not included in any namespace code, it contains an empty string. Constants are __NAMESPACE__ useful when creating names dynamically.

Keyword namespace can be used to explicitly access elements in the current namespace or child namespace. It is equivalent to an operator in a class self .

Using namespaces: Aliases/imports

Allows the external fully qualified name to be referenced or imported through an alias, similar to the ability to create symbolic connections to other files or directories in a UNIX-like file system.

The PHP namespace supports two aliases or methods of importing:

    • Use aliases for class names
    • Use aliases for namespace names

Use use the operator to import or use aliases to support a row containing multiple use statements. Note the names in the namespace, leading backslashes are unnecessary and do not allow backslashes, because the imported name must be fully qualified and will not be resolved relative to the current namespace.

Global space

If no namespaces are defined, all classes and functions are defined in the global space. Precede the name with a prefix that \ indicates that the name is the name in the global space.

Using namespaces: Fallback global functions/constants

In a namespace, when PHP encounters an unqualified class, function, or constant name, a different wired policy is used to resolve the name.

The class name is always resolved to the name in the current namespace, so you must use the fully qualified name when accessing the name of a class that is inside the system or that is not included in the namespace.

For functions and constants, if the function or constant does not exist in the current namespace, PHP will fallback to use the function or constant in the global space.

Name resolution rules

The name resolution rule text is quite many, need concrete problem concrete analysis.

(End of full text)

The above introduces a good PHP library can not be separated from a good namespace, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.