PHP namespace learning detailed _php tips

Source: Internet
Author: User

1, what is a namespace?
A namespace is a special scope that contains identifiers that are located under that scope, and which is itself an identifier. You can match the namespace to the directory of the operating system. A namespace is the equivalent of a directory, a class in a namespace, a function, a constant, and a file in a directory. The file names in the same directory (namespace) cannot be the same, but different directories can have files of the same name.
2, using namespaces to solve what problem?
Resolve name conflicts, such as defining a class, which is exactly the same as the class in PHP or a class library included in.
To improve code readability, namespaces have an alias feature that helps you alias a more than 10-character class name to shorten the code without worrying about naming conflicts with other spaces.
3, which code will be affected by the namespace.
Three classes: class, function, constant. Only their brothers are affected, the others do, and why. When it comes to constants, PHP 5.3 can then use the Const keyword to define constants, 5.3 This uses define before, and the namespace is only valid for the Const keyword.
4, how to define the namespace

Copy Code code as follows:

namespace MyProject;
Const CONNECT_OK = 1;//php5.3 After
Class Connection {/* ... */}
function connect () {//* ... */}
#例子二
namespace Myprojectsublevel;
Const CONNECT_OK = 1;//php5.3 After
Class Connection {/* ... */}
function connect () {//* ... */}

Use ' namespace space name ' to declare a space, before namespace in addition to declare statements can not have any other PHP statements, but also can not have any non-PHP code, even the space can not have.
The following are the wrong forms:
Copy Code code as follows:

$a = 1;
namespace MyProject;
? >www.jb51.net
Fatal Error:namespace declaration statement has to be the very the script ...

In addition, the same namespace can be defined in multiple files, which is useful for the organizational framework. Files that start with the same namespace MyProject, which are the same namespace. So note that the file may not have the same class/function/constant name.
Of course, the same file can also define multiple namespaces, but it is highly recommended. (Understanding the same file defines multiple namespaces)
5, how to use the namespace
Namespaces have three forms of use:
. Unqualified name-use no delimiter, directly using class/function/constant name, such as: New Foo (); Foo (); Echo FOO; When a file has a namespace used,
Copy Code code as follows:

<?php
namespace MyObject;
New Foo (); Call Myobjectfoo ();
Foo (); Call Myobjectfoo ();
Echo FOO; Call Myobjectfoo;

Non-fully qualified name-not beginning with a delimiter, such as New Subfoo (); This form is the same as the unqualified name method.
Copy Code code as follows:

<?php
namespace MyObject; New Subfoo (); Call Myobjectsubfoo ();

Fully qualified name-a way to begin with a separator, equivalent to an absolute address in an operating system. such as new Othernsfoo ();
Copy Code code as follows:

<?php
namespace MyObject; New Othernsfoo (); Call Othernsfoo (); Regardless of the MyObject namespace.

Tip: There is also a special place for functions and constants (fallback global functions/constants).
Copy Code code as follows:

<?php
namespace MyObject;
FuncName (); Call Myobjectfuncname () if the myobjectfuncname exists, or try to invoke funcname (); Echo FOO; Ditto.

For classes, there is also a special place.
Copy Code code as follows:

<?php
namespace MyObject;
New Foo (); * If Myobjectfoo exists, call it if it does not exist, call __autoload try to load the Myobjectfoo class in.
Note that for classes it is not automatic to invoke a class under the global scope.

Previously said, namespaces also have a purpose--alias.
Copy Code code as follows:

namespace MyObject;
Use Othernssub as other;
Use OTHERNSSUB2; Equivalent to use OTHERNSSUB2 as SUB2;
Use/myclass;
New Foo (); Call Myobjectfoo ();
New Otherfoo (); Call Othernssubfoo ();
New Sub2foo (); Call Othernssub2foo ();
New MyClass (); Call MyClass ();

6. Dynamic namespaces
Dynamic can always be confusing, yet it brings flexibility. Namespaces can also use dynamic language features, but note that because a direct call to a namespace is a compile-time resolution, dynamic features are not compile-time parsing. So be sure to prefix it. Such as:
Copy Code code as follows:

namespace Myobjectsub;
New Foo (); Call Myobjectsubfoo (), which has been parsed into myobjectsubfoo at compile time
$a = ' Foo ';
New $a (); The call is foo (), not Myobjectsubfoo ()
$b = ' Myobjectsubfoo '; Equivalent to Myobjectsubfoo
New $b (); Call Myobjectsubfoo ()
If you use double quotes, use \, such as $a = "\myobject\sub";

Appendix 1: The same file defines multiple namespaces
There are two kinds of methods:

Copy Code code as follows:

namespace MyProject;
Const CONNECT_OK = 1;
Class Connection {/* ... */}
function connect () {//* ... */}
namespace Anotherproject;
Const CONNECT_OK = 1;
Class Connection {/* ... */}
function connect () {//* ... */}

Method One, record the water account.

Copy Code code as follows:

Namespace MyProject {
Const CONNECT_OK = 1;
Class Connection {/* ... */}
function connect () {//* ... */}
}
Namespace Anotherproject {
Const CONNECT_OK = 1;
Class Connection {/* ... */}
function connect () {//* ... */}
}
namespace {//Global
Const CONNECT_OK = 1;
Class Connection {/* ... */}
function connect () {//* ... */}
}

Method Two, use curly braces to place the same namespace code in braces. This method requires that there be no code other than declare outside the curly braces. The code for the global scope is surrounded by braces with no space name.

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.