PHP object-oriented Programming (OOP) learning notes (v)-PHP namespace _php instance

Source: Internet
Author: User
Tags aliases constant

Namespaces Overview

In PHP, namespaces are used to address two types of problems when creating reusable code such as classes or functions when writing a class library or application:

User-written code conflicts with the name of a class/function/constant or third party class/function/constant inside PHP.
Create an alias (or short) name for a long identifier name (usually defined to mitigate the first type of problem), improving the readability of the source code.
The PHP namespace provides a way to combine related classes, functions, and constants. Here is an example that describes the syntax of the PHP namespace:

Defining namespaces

Although any legitimate PHP code can be contained within a namespace, only three types of code are affected by namespaces: classes, functions, and constants. Namespaces are declared by keyword namespace. If a file contains namespaces, it must declare the namespace before all other code. In addition, unlike other language features in PHP, the same namespace can be defined in multiple files, allowing the content of the same namespace to be split into separate files. Of course, you can also define multiple namespaces in the same file.

Copy Code code as follows:

namespace MyProject;
Class MyClass
{
#code ...
}

Defines a child namespace: the relationship to directories and files is much like the PHP namespace allows you to specify the name of a hierarchical namespace. Therefore, the name of the namespace can be defined in a hierarchical way:

Copy Code code as follows:

namespace Myproject\helper\http;
Class MyClass
{
#code ...
}

Defining multiple namespaces in the same file: There are two ways to declare multiple namespaces in the same file, but in actual programming practice, it is highly discouraged to define a multiple-GE namespace in the same file. This approach is primarily used to merge multiple PHP scripts into the same file. The first method is listed below.

Copy Code code as follows:

namespace Myproject\helper\http;
Class MyClass
{
#code ...
}
namespace Myproject\helper\request;
Class MyClass
{
#code ...
}

However, this method is strongly not recommended and can be referenced in the following braces definition method:

Copy Code code as follows:

namespace Myproject\helper\http;
{
Class MyClass
{
#code ...
}
}
namespace Myproject\helper\request;
{
Class MyClass
{
#code ...
}
}

Elements in the PHP namespace use

Before discussing how to use namespaces, you must understand how PHP knows what elements to use in the namespace. Class names can be referenced in three different ways:

An unqualified name, or a class name that does not contain a prefix, such as $a =new foo (); or Foo::staticmethod ();. If the current namespace is Currentnamespace,foo, it will be resolved to Currentnamespace\foo. If the code that uses Foo is global and does not contain code in any namespace, Foo is resolved to Foo. Warning: If a function or constant in the namespace is undefined, the unqualified function name or constant name is resolved to the global function name or constant name. See Use namespaces: Backing global function name/constant name.

A qualified name, or a name that contains a prefix, such as $a = new Subnamespace\foo (); or Subnamespace\foo::staticmethod ();. If the current namespace is Currentnamespace, Foo is resolved to Currentnamespace\subnamespace\foo. If the code that uses Foo is global and does not contain code in any namespace, Foo will be parsed as Subnamespace\foo.

Fully qualified name, or contains the name of the global prefix operator, for example, $a = new \currentnamespace\foo (); or \currentnamespace\foo::staticmethod ();. In this case, Foo is always resolved to the literal name (literal name) Currentnamespace\foo in the code.
Using namespaces: Alias/import

It is an important feature of a namespace to allow fully qualified names that can be referenced or imported by an alias. There are two ways to use aliases or imports for PHP namespace support: Use aliases for class names, or use aliases for namespace names. In PHP, aliases are implemented using the operator use.

Note PHP does not support import functions or constants.

Copy Code code as follows:

namespace Foo;
Use My\full\classname as Another;

The following example is the same as the use My\full\nsname as Nsname
Use My\full\nsname;

Import a global class
Use \arrayobject;

Name resolution rules

Before we explain the name resolution rule, let's look at some important definitions:

Unqualified name Unqualified name: An identifier that does not contain a namespace separator in the name, such as Foo
Qualified name qualified name: an identifier that contains a namespace delimiter in the name, such as Foo\bar
The fully qualified name fully qualified name: a name that contains a namespace delimiter and an identifier that starts with a namespace delimiter, such as \foo\bar. Namespace\foo is also a fully qualified name.
Name resolution follows these rules:

Calls to fully qualified names, classes, and constants are parsed at compile time. For example, the new \a\b resolves to a class a\b.
All unqualified and qualified names (not fully qualified names) are converted at compile time according to the current import rule. For example, if the namespace a\b\c is imported as C, then the call to C\d\e () is converted to a\b\c\d\e ().
Inside a namespace, all qualified names that are not converted according to the import rule precede them with the current namespace name. For example, C\d\e () is called inside the namespace a\b, then C\d\e () is converted to a\b\c\d\e ().
Unqualified class names are converted at compile time based on the current import rule (replace the short import name with the full name). For example, if the namespace a\b\c is imported as C, the new C () is converted to the new a\b\c ().
Within a namespace (for example, a\b), a function call to an unqualified name is resolved at run time. For example, the call to function foo () is parsed like this:
1 to find a function named A\b\foo () in the current namespace
2 try to find and call function foo () in global space.
Calls to unqualified or qualified name classes (not fully qualified names) within a namespace (such as a\b) are resolved at run time. The following is the parsing process for calling new C () and New D\e (): Resolution of New C ():
Finds the A\b\c class in the current namespace.
Try to load the class a\b\c automatically.

Resolution of New D\e ():
Precede the class name with the current namespace name into: A\b\d\e, and then look for the class.
Try to load the class a\b\d\e automatically.

In order to reference global classes in the global namespace, you must use the fully qualified name new \c ().

Example Name Resolution Example

Copy Code code as follows:

<?php
namespace A;
Use b\d, c\e as F;
Function call
Foo (); First attempt to invoke function foo () defined in the namespace "a"
Try calling global function "foo" again
\foo (); Call Global space function "Foo"
My\foo (); Call definition function "foo" in Namespace "A\my"
F (); First try to invoke the function "F" defined in the namespace "a"
Try calling the global function "F" again
Class reference
New B (); Creates an object of class "B" defined in the namespace "a"
If not found, try to mount the class "a\b" Automatically
New D (); Use an import rule to create an object of class "D" defined in the namespace "B"
If not found, try to mount the class "b\d" Automatically
New F (); Use an import rule to create an object of class "E" defined in the namespace "C"
If not found, try to mount the class "C\e" Automatically
New \b (); To create an object that defines the class "B" in the global space
If not found, try to load the class "B" Automatically
New \d (); To create an object that defines the class "D" in the global space
If not found, try to mount the class "D" Automatically
New \f (); To create an object that defines the class "F" in global space
If not found, try to mount the class "F" Automatically
Call a static method or a namespace function in another namespace
B\foo (); Invoke the function "foo" in the namespace "a\b"
B::foo (); Invoke the "Foo" method of the class "B" defined in the namespace "A"
If the class "a\b" is not found, attempt to automatically mount the class "a\b"
D::foo (); Use the import rule to invoke the "Foo" method of the class "D" defined in the namespace "B"
If the class "b\d" is not found, try to automatically mount the class "b\d"
\b\foo (); Call function "Foo" in Namespace "B"
\b::foo (); Call the "Foo" Method of Class "B" in global space
If class "B" is not found, try to mount the class "B" automatically
static methods or functions in the current namespace
A\b::foo (); Invoke the "Foo" method of the class "B" defined in the namespace "A\a"
If the class "a\a\b" is not found, try to automatically mount the class "a\a\b"
\a\b::foo (); Invoke the "Foo" method of the class "B" defined in the namespace "a\b"
If the class "a\b" is not found, try to automatically mount the class "a\b"
?>

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.