PHP命名空間規(guī)則解析及高級功能_第1頁
PHP命名空間規(guī)則解析及高級功能_第2頁
PHP命名空間規(guī)則解析及高級功能_第3頁
PHP命名空間規(guī)則解析及高級功能_第4頁
PHP命名空間規(guī)則解析及高級功能_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、php命名空間規(guī)則解析及高級功能one of the most important new features of php 5. 3, released recently, is the addition of namespaces php namespace some terms has been introduced in this paper, the parsing rules, as well as the application of some advanced features, hoping to help the reader actually use namespace i

2、n the projecthere we introduced the php namespace in the purpose and the namespace keyword, in this article we will introduce the use command to use and how to parse php namespace namefor comparison purposes, i've defined two pieces of code that are almost identicsl, except for namespaces.1. <

3、;? php/ / application library 13. the namespace app libi;const myconst =,app libi myconst,;the function myfunction () 6 returnfunction 7. 8. the class myclass static function whoami () 10 eturn _method_;11. 12. 13. ? >lib2 php1. <? php/ / application library 23. the namespace app lib2;4.const

4、myconst 二'app lib2 myconst,;6.()7. function myfunction ()the return _function_;11. the class myclass the static function whoami () 13.14. 15. 16. ? >to begin with, you need to understand a few php namespacesfully qualified name (fully qualified name)any php code can refer to a fully qualified

5、 name, which is an identifier starting with a namespace backslash, such as app libi myconst, app lib2 myfunction ().fully qualified name is no ambiguity, the beginning of the backslash and the function of the file path is similar, it said the root" global space, if we in the global space implem

6、ents a different myfunction (), you can use myfunction () from libl. php or lib2. php call it.fully qualified name is very useful for one-time initialization function call or object, but when you have a lot of calls they have no practical value, in the following discussion, we will see the php provi

7、des other options to release our worry about namespaces typingqualified namethere is at least one identifier for the namespace separator, such as libi myfunction ()in pieces the qualified name (unqualified name)no namespace separator identifier, such as myfunction ()working within the same namespace

8、consider the following code:myappl php1. <? php2. the namespace app libi;3.require_once (' lib 1. php ');require_once (' lib2. php ');6.header c content-type: text/plain ');echo myconst n;echo myfunction ()" rt;10. echo myclass: : who am i (). " n"11. ? >even

9、 if we include libl. php and lib2. php, myconst, myfunction and myclass identifier can only be referenced in lib1. php, because the code for myapp1. php is in the same app libl namespacethe results:1. the app libl myconst2. the app libl myfunctionapp libl myclass: : whoaminamespace importyou can imp

10、ort the namespace using the use operator, such as:myapp2 php1. <? php2. use app lib2;require_once (' lib 1. php ');5.require_once (' lib2. php ');6.header c content-type: text/plain ');echo lib2 myconstecho lib2 myfunction ()10. echo lib2 myclass: : whoami (). " n;11. ? &

11、gt;can define any number of use statement, or use a comma to separate namespace, in this case we import the app lib2 namespace, but we still can,t directly quoting myconst, myfunction and myclass, because our code is in the global space, but if we add the lib2 prefix, they become a qualified name, p

12、hp will search the imported namespace, until a match is found.the results:1. the app lib2 myconst2. the app lib2 myfunctionapp lib2 myclass: : whoaminamespace aliasnamespace aliases are probably the most useful ideas, and aliases allow us to refer to very long namespaces using shorter namesmyapp3 ph

13、p1. <? phpuse app libi as l;use app lib2 myclass as obj;4.the header c content-type: text/plain ');require_once (' lib 1. php ');require_once (' lib2.');&echo l myconst.10. echo l myfunction ().11. echo l myclass: : who am i (). " rt;echo obj: : whoami (). " n;13

14、.? >first use statement defines the app libi asany use ofqualified name at compile time will be translated into "app libl,so we can refer to the l myfunction myconst and l not fully qualified namethe second use statement defines the obj as app lib2 namespace alias of myclass class in this wa

15、y, only suitable for class, cannot be used with constants and functions, we can now use the new obj () or run a static method as abovethe results:1. the app libi myconst2. the app libi myfunctionapp libi myclass: : whoamiapp lib2 myclass: : whoamiphp name parsing rulesthe php identifier name is pars

16、ed using the following namespace rules refer to the php user manual for more details:calling fully qualified functions, classes, or conslants at compile time;2. the qualified name and qualified name translation according to the rules of the import, for example, if a b c import for c, call c, d, e ()

17、 will be translated into a b, c, d, e 0;3. within the php namespaces, all qualified name is according to the rules of the import transformation, for example, if the namespace is called a b c, d, e (), then will be translated into a b, c, d, e ();4. the qualified class name according to the current i

18、mport rules, use full name to replace the imported short name, for example, if a class c in the namespace a b to be imported for x, then the new x () will be translated into new a b c ():5. in the namespace qualified function calls resolved at run time, for example, if myfunction () is called in the

19、 namespace a b, php will first lookup function b myfunction (), if not found, then go back to look for in the global space myfunction ();6. call an unqualified or qualified class name is resolved at run time, for example, if we call new c in the namespace a b (), php w訂 1 find the class a, b, c, if

20、not found, php will attempt to automatically load a b cphp namespace advaneed featureslet's take a look at some of the adv a need features of the pup namespacethe _namespace_ constant_namespace_ is a php string that always returns the name of the current namespace,in the global space it is an em

21、pty string1. <? php2. the namespace app libi;echo _naiespace_ / / work: app libi3. ? >5.this value is useful when debugging, and it can also be dynamically generated by a fully qualified class name such as:1. <? php2. the namespace app libi;3.public function whoami () 6 return _method_7. &a

22、mp;9.$c = _namespace_' myclass,.$m 二 new $c:echo $m-> who am i () / / outputs: app libi myclass: : who am i13.? >the namespace keywordthe namespace keyword can be used to explicitly reference a project in a current namespace or subnamespace, which is equivalent to the self namespace in the

23、 class:1. <? php2. the namespace app libi;3.public function whoami () 6 return _method_4. 5. 9.$m 二 new namespace myclass;echo $m-> who am i () ; / / outputs: app libi myclass: : who am i12. ? >automatically load the namespace classthe most time-saving feature of php 5 is auto-load, and in

24、the global (non-namespace) php code, you can write a standard auto-load function:1. <? php$obj 二 new myclassl () ; / / classes/myclassl php is auto-loaded$obj 二 new myclass2 () ; / / classes/myclass2. php is auto-loaded/ / autoload functionfunction _autoload ($class_name) require_once (“ classes

25、/ $class_name. php “);&6. ? >in php 5. 3, you can create a namespace instance of the class, in this case, the fully qualified namespace and name of the class passed to _autoload function, for example, $class_name value could be app libi myclass you can place all the php class files in the sam

26、e folder, and extract the namespace from the string, but that will cause file name collisionsin addition, your class file hierarchy will be reorganized according to the structure of the namespace, for example, the myclassphp file can be created in the/classes/app/libl folder:/ classes/app/libl / myclass php1. <? php2. the namespace app libi;3.public function whoami () 6 return m

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論