A good PHP library is inseparable from a good namespace

Source: Internet
Author: User
: This article mainly introduces that a good PHP library is inseparable from a good namespace. For more information about PHP tutorials, see. Zookeeper

A good PHP library is inseparable from a good namespace overview

A namespace is a way to encapsulate things. This abstract concept can be seen in many places. for example, in the operating system, directories are used to group related files. for files in directories, directories play the role of namespaces. This principle is applied to the field of programming as a namespace concept. PHP 5.3 and later versions support namespaces.

Define a namespace

Keyword used by the namespacenamespace. Any valid PHP code can be included in the namespace, but only the class, function, and constant types are affected by the namespace.

The only valid code before declaring a namespace is used to define the source file encoding method.declareStatement. The namespace must be the first statement of the program script. all non-PHP code, including blank characters, cannot appear before the namespace declaration. The same namespace can be defined in multiple files.

Define a sub-namespace

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

Define multiple namespaces in the same file

PHP supports this syntax, but it is not recommended. Defining multiple namespaces in the same file has two syntax forms: a simple listing and a syntax in the form of braces. The second type is recommended.


  

The code in a global non-namespace is combined with the code in the namespace. you can only use the syntax in braces. Nantong longcard game Hall http://www.szjmswy.com/

Use namespace: Basic

To use a namespace, you must understand how PHP knows the elements in the namespace to be used.

In a file system, there are three methods to access a file:

  • Relative file name format
  • Relative path name format
  • Absolute path name format

Similar to the file system, in the PHP namespace, class names can be referenced in three ways. you need to understand the namespace name definition:

  • A non-qualified name that does not contain a prefix. The name does not contain the namespace separator identifier. For exampleFoo.
  • The name that contains the prefix. the name contains the identifier of the namespace separator. For exampleFoo\Bar.
  • Fully qualified name, including the name of the global prefix operator. The name contains the namespace separator and the identifier starting with the namespace separator. For example\Foo\Bar.namespace\FooIt is also a fully qualified name.

Namespaces and dynamic language features

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

namespaceKeyword and__NAMESPACE__Constant

The constant _ NAMESPACE _ value is a string containing the name of the current NAMESPACE. Globally, code that is not included in any namespace contains an empty string. Constant__NAMESPACE__It is useful when dynamically creating names.

KeywordsnamespaceIt can be used to explicitly access elements in the current namespace or sub-namespace. It is equivalentselfOperator.

Use namespace: Alias/import

Allows alias reference or external fully qualified names, similar to creating symbolic connections to other files or directories in unix-like file systems.

PHP namespaces support two alias or import methods:

  • Use aliases for class names
  • Use an alias for the namespace name

UseuseThe operator imports or uses aliases. a single line can contain multipleuseStatement. Note that for the name in the namespace, the leading backslash is unnecessary or not allowed, because the import name must be fully qualified, it is not parsed relative to the current namespace.

Global Space

If no namespace is defined, all classes and functions are defined in the global space. Prefix before name\Indicates that the name is in the global space.

Use namespace: Reserve global functions/constants

In a namespace, when PHP encounters an unqualified class, function, or constant name, it will be parsed using different wired policies.

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

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

Name resolution rules

The name resolution rules have many texts and need to be analyzed in detail.

(Full text)

The above introduces that a good PHP library cannot be separated from a good namespace, including some content, and I hope to be helpful to friends who are interested in PHP tutorials.

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.