第十四節--命名空間 <?php namespace core_php:utility { class textEngine { public function uppercase($text) //大寫 { return(strtoupper($text)); } } //make non-OO interface 建立一個非OO的接口 function uppercase($text) { $e = new textEngine; return($e->uppercase($text)); } } //test class in namespace 測試命名空間中的類 $e = new core_php:utility::textEngine; print($e->uppercase("from object") . "<br>"); //test function in namespace 測試命名空間中的函數 print(core_php:utility::uppercase("from function") . "<br>"); //bring class into global namespace 把類導入全局命名空間 import class textEngine from core_php:utility; $e2 = new textEngine; ?> Import語句把命名空間中的某個部份導入全局的命名空間. 要導入單一的命名空間的成員,可以指定類型為constant,function或class,接著寫上成員的名稱; //如import class XXX 如果你想導入某一特定類型的所有成員,你可以用*來代替名稱; //如 import constant * 導入所有常量 如果你想導入所有類型的所有成員,用*即可. //如 import * 在成員之后,用from關鍵字加上命名空間的名稱. //如 import class textEngine from core_php:utility; 總之你要寫成像import * from myNamespace或 import class textEngine from core_php:utility這樣的語句,就像例6.17中那樣. |
溫馨提示:喜歡本站的話,請收藏一下本站!