PHP5.3 namespace Namespace Translation

Source: Internet
Author: User
Tags constant

PHPnamespace is used to solve the scope problem in large PHP libraries (libraries), in PHP, all class definitions are global, so when a library author creates multiple applications or public API classes for this library, He must be aware of the possibility of similar functions in other libraries and therefore choose a unique name to ensure that the libraries can be used concurrently. Usually a unique string prefix method is used to resolveDatabaseThe class is called my_library_db, and so on. When this library is incremented, the prefix increases, resulting in a long name. Namespaces enable developers to manage named Van without using long names each time they reference a class
Surrounding.

Defined
Namespaces are declared at the beginning of each file with the namespace keyword, as
<?php
namespace MyProject::D b;

Const CONNECT_OK = 1;
Class Connection {/* ... */}

function connect () {//* ... */}

?>
The same namespace name can be used in multiple files
Namespace package class,constant and function definitions, but no free code
Detailed
Inside the namespace, all defined class,function and constant names are automatically labeled with namespace names, and the class names are always the complete names, such as the classes in the above example use MyProject::D b::connection
To invoke
A constant that is created by a namespace name plus a constant name, like a class constant, and a namespace constant that can only be a static constant.

Non-complete decorated names, such as names that do not contain::), are followed as follows:
Non-automatic load classes find in the current namespace (with namespace name)
Find in non-auto load global namespaces
autoloading for name in current namespace is attempted.
If the former lookup fails, the lookup fails

Incomplete decorated function names, such as names that do not include::) first find in the current namespace and then find in global space
Incomplete cosmetic constant name is found in the current namespace and then found in globally defined constants

Using namespaces
Classes and functions in each namespace can be referenced using the full name, such as MyProject::D b::connection, or MyProject::D b::connection
<?php
Require ' myproject/db/connection.php ';
$x = new MyProject::D b::connection;
MyProject::D b::connect ();
?>
Namespaces can be introduced into the current context (global or namespace) using the use operator, and the operator parameters are as follows:
<?php
/* ... */
Use Some::name as Othername;
Simplified application
Use Foo::bar;
The same
Use Foo::bar as Bar;
?>
The introduced name works as follows: Each time the interpreter encounters a local name Othername (single name or being:: delimited long name), the introduced name Some::name is substituted
Use can only be used in a global scope, not a function or class, the introduced name is valid from the beginning of the introduction to the end of the current file, recommended to be introduced at the beginning of the file to avoid conflict
<?php
Require ' myproject/db/connection.php ';
Use MyProject::D b;
Use MyProject::D b::connection as DbConnection;

$x = new MyProject::D b::connection ();
$y = new Db::connection ();
$z = new DbConnection ();
Db::connect ();
?>
Global space
When there is no namespace definition, all classes and functions are placed in the global space, because this is the case before PHP supports namespaces.
In a namespace context, called:: Will specify the name from the global space
<?php
namespace A::b::c;

/* This function is A::b::c::fopen * *
function fopen () {
/* ... */
$f =:: fopen (...); Global functions
return $f;
}
?>
__namespace__
The constant __namespace__ of the interpretation period is defined as the name of the current namespace, outside the namespace, and the constant value is an empty string.
<?php
namespace A::b::c;

function foo () {
Do stuff
}
Set_error_handler (__namespace__. ":: foo");
?>
Rules
1. All fully decorated names are transformed by the current introduction of rules, such as A::B::C has been introduced, C::D:: E () is converted to A::B::C::D:: E ().
2. The incomplete modifier name is transformed by the current introduction of the rule (the name replaces the simple name), as A::b::c has been introduced, and the new C () is converted to A::B::C ().
3. Within the namespace, calls to the incomplete decorated function names defined within the current namespace are considered to be functions that call those namespaces during interpretation
4. Within the namespace (A::B), the invocation of a nonholonomic decorated name function that is not defined in the current namespace is determined during runtime, and the following is how Foo () is determined
Looks for function a::b::foo () within the current namespace.
Try to find and invoke the internal (internal) function foo ()
Call user-defined functions in global space, using:: Foo ()
5. Within the namespace (A::B), the invocation of a nonholonomic decorated name class not defined within the current namespace is determined during runtime, and the following is how the new C () is determined.
Finding classes in the current namespace A::b::c
Try to find and invoke internal class C
Try to load A::b::c automatically
Call user-defined classes in global space, using NEW:: C ()
6. Functions that invoke the full decorated name are determined during the run. The following is how A::b::foo () is determined
Looks for function a::b::foo () within the current namespace.
Call user-defined functions in global space, using:: Foo ()
7. When you invoke a class with a fully decorated name, you are loading it at run time in the namespace you want to correspond to. Like the new A::b::c () it is the reference namespace is a::b under the C () class
<?php
namespace A;
Function call
Foo (); Try calling the "foo" defined in namespace a first
Call the internal (internal) function "Foo" again
:: foo (); Call Global-scoped "foo"
Class reference
New B (); First try to create an object of Class B defined in namespace a
Create an internal Class B object again
NEW:: B (); New object for Class B of the global domain definition
Static methods/namespaces from other namespaces
B::foo (); Try calling the function foo () from the namespace "a::b" first
Then call the Inner class B function foo ()
:: B::foo (); Try calling function foo () from namespace B first.
And then call the function in B in the global Domain foo ()
Static methods/namespaces for the current namespace
A::foo (); Invoke function foo () in the namespace "a::a" first
Then call the function of Class A in namespace A, foo ()
And then try calling namespace a function foo ()
Then call the function in the inner Class A, foo ()
:: A::foo (); First try calling namespace a function foo ()
Then call the function of Class A in the Global domain foo ()
?>

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.