Questions and Techniques for PHP namespaces

Source: Internet
Author: User
Tags constant php code

  This article mainly introduces the problems and techniques of the PHP namespace, and the friends who need it can refer to the following

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   code as follows: namespace MyProject; Const CONNECT_OK = 1;//php5.3 later class Connection {//} function CONNECT () {//* ... * * &nbsp} #例子二 namespace Mypro Jectsublevel; Const CONNECT_OK = 1;//php5.3 later 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 is the wrong form: The   code is as follows: $a = 1; namespace MyProject >www.jb51.net//fatal error:namespace declaration statement-has to be the very-I n the script ...   the other same namespace can be defined in multiple files, which is useful for the organization framework. that the sameNamespace MyProject a file that starts with 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 namespaces can be used in three different forms:. 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, the   code is as follows: <?php namespace MyObject; New Foo (); Call Myobjectfoo (); Foo (); Call Myobjectfoo (); Echo FOO; Call Myobjectfoo;        Unqualified name-not beginning with a separator, 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 ();   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).   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.   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.   said before that the namespace alsoThere is one use-alias.   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 namespace dynamics 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: The Code is as follows: namespace Myobjectsub; New Foo (); Call Myobjectsubfoo (), the compile-time has been parsed into myobjectsubfoo $a = ' Foo '; New $a (); The call is foo (), not myobjectsubfoo () $b = ' Myobjectsubfoo '; Equivalent to Myobjectsubfoo new $b (); Call Myobjectsubfoo ()//if using double quotes, use, such as $a = "myobjectsub";     1: The same file defines multiple namespace methods: The code is as follows: namespace MyProject; Const CONNECT_OK = 1; Class Connection {/* ... */} function connect () {//* ... * * *  } namespace Anotherproject; Const CONNECT_OK = 1; Class Connection {/* ... */} function connect () {/* ... */&nbsp}     method One, record the bill of water.     Code as follows: namespace MyProject {const CONNECT_OK = 1; class Connection {/*.../} function connect () {/////&NBSP}} namespace Anotherproject {const CONNECT_OK = 1; class Connection {* *. .. /{/} function connect () {/*/&nbsp}} namespace {//Global const CONNECT_OK = 1; class Connection {/* ... */} FUNCTI On connect () {///&nbsp}}     method Two, use curly braces to enclose the same namespace code in curly braces. This method requires that there be no code other than declare outside the curly braces. For global scope code to surround with 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.