PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之命名空間與自動(dòng)加載類(lèi)詳解
本文實(shí)例講述了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之命名空間與自動(dòng)加載類(lèi)。分享給大家供大家參考,具體如下:
命名空間
避免類(lèi)名重復(fù),而產(chǎn)生錯(cuò)誤。
<?php require_once "useful/Outputter.php"; class Outputter { // output data private $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } } $obj = new Outputter(); // 同一命名空間下,類(lèi)名不能相同,默認(rèn)命名空間為空。空也是一種命名空間。 $obj -> setName("Jack"); print $obj->getName(); //namespace useful; // 更改命名空間,否則查詢(xún)不到Hello類(lèi),Fatal error: Class 'my\Hello' not found $hello = new Hello(); ?> <?php // useful/Outputter.php namespace useful; // 命名空間 class Outputter { // } class Hello { } ?>
如何調(diào)用命名空間中的類(lèi)
<?php namespace com\getinstance\util; class Debug { static function helloWorld() { print "hello from Debug\n"; } } namespace main; // com\getinstance\util\Debug::helloWorld(); // 找不到Debug類(lèi) \com\getinstance\util\Debug::helloWorld(); // 加斜杠之后,就從根部去尋找了。 // outPut:hello from Debug ?>
使用use關(guān)鍵字
<?php namespace com\getinstance\util; class Debug { static function helloWorld() { print "hello from Debug\n"; } } namespace main; use com\getinstance\util; //Debug::helloWorld(); //Fatal error: Class 'main\Debug' not found util\Debug::helloWorld(); ?>
使用下面的處理,直接可以調(diào)用類(lèi)
<?php namespace com\getinstance\util; class Debug { static function helloWorld() { print "hello from Debug\n"; } } namespace main; use com\getinstance\util\Debug; // 直接使用到類(lèi) Debug::helloWorld(); ?>
\表示全局
global.php
<?php // no namespace class Lister { public static function helloWorld() { print "hello from global\n"; } } ?> <?php namespace com\getinstance\util; require_once 'global.php'; class Lister { public static function helloWorld() { print "hello from ".__NAMESPACE__."\n"; // __NAMESPACE__當(dāng)前namespace } } Lister::helloWorld(); // access local \Lister::helloWorld(); // access global ?>
輸出:
hello from com\getinstance\util
hello from global
命名空間加{}
<?php namespace com\getinstance\util { class Debug { static function helloWorld() { print "hello from Debug\n"; } } } namespace main { \com\getinstance\util\Debug::helloWorld(); } ?>
output:
hello from Debug
全局命名空間
<?php namespace { // 全局空間 class Lister { public static function helloWorld() { print "hello from global\n"; } } } namespace com\getinstance\util { class Lister { public static function helloWorld() { print "hello from ".__NAMESPACE__."\n"; } } Lister::helloWorld(); // access local \Lister::helloWorld(); // access global } ?>
__autoload 自動(dòng)加載類(lèi)
ShopProduct.php
<?php class ShopProduct { function __construct() { print "ShopProduct constructor\n"; } } ?> <?php function __autoload( $classname ) { // 自動(dòng)加載,根據(jù)類(lèi)名加載類(lèi) include_once( "$classname.php" ); } $product = new ShopProduct( 'The Darkening', 'Harry', 'Hunter', 12.99 ); ?>
output:
ShopProduct constructor
進(jìn)一步優(yōu)化處理
位于文件夾business/ShopProduct.php
<?php class business_ShopProduct { // 這里的類(lèi)命名就要遵循規(guī)則了 function __construct() { print "business_ShopProduct constructor\n"; } } ?> <?php function __autoload( $classname ) { $path = str_replace('_', DIRECTORY_SEPARATOR, $classname ); // 智能化處理 require_once( "$path.php" ); } $x = new ShopProduct(); $y = new business_ShopProduct(); ?>
output:
ShopProduct constructor
business_ShopProduct constructor
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP基本語(yǔ)法入門(mén)教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP命名空間和自動(dòng)加載類(lèi)
- PHP中類(lèi)的自動(dòng)加載的方法
- PHP實(shí)現(xiàn)的簡(jiǎn)單路由和類(lèi)自動(dòng)加載功能
- PHP命名空間與自動(dòng)加載類(lèi)詳解
- 解析php類(lèi)的注冊(cè)與自動(dòng)加載
- php _autoload自動(dòng)加載類(lèi)與機(jī)制分析
- php類(lèi)的自動(dòng)加載操作實(shí)例詳解
- php類(lèi)自動(dòng)加載器實(shí)現(xiàn)方法
- PHP類(lèi)的自動(dòng)加載機(jī)制實(shí)現(xiàn)方法分析
- PHP MVC框架中類(lèi)的自動(dòng)加載機(jī)制實(shí)例分析
- PHP命名空間(Namespace)的使用詳解
- PHP類(lèi)的自動(dòng)加載與命名空間用法實(shí)例分析
相關(guān)文章
PHP實(shí)現(xiàn)批量清空刪除指定文件夾所有內(nèi)容的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)批量清空刪除指定文件夾所有內(nèi)容的方法,涉及php基于自定義函數(shù)遞歸調(diào)用實(shí)現(xiàn)刪除指定目錄下文件與文件夾相關(guān)操作技巧,需要的朋友可以參考下2017-05-05php文件打包 下載之使用PHP自帶的ZipArchive壓縮文件并下載打包好的文件
php文件打包 下載之使用PHP自帶的ZipArchive壓縮文件并下載打包好的文件2012-06-06php中使用__autoload()自動(dòng)加載未定義類(lèi)的實(shí)現(xiàn)代碼
當(dāng)PHP引擎遇到未實(shí)例化的類(lèi)時(shí)就會(huì)觸發(fā)這個(gè)方法,當(dāng)然你的php代碼中要用到__autoload()才可以哦2013-02-02PHP獲取當(dāng)前系統(tǒng)時(shí)間的方法小結(jié)
這篇文章主要介紹了PHP獲取當(dāng)前系統(tǒng)時(shí)間的方法小結(jié),需要的朋友可以參考下2018-10-10教你如何在CI框架中使用 .htaccess 隱藏url中index.php
CodeIgniter(以下簡(jiǎn)稱(chēng)"CI")是一款國(guó)外優(yōu)秀的PHP輕量級(jí)MVC框架,它支持PHP4和PHP5,是開(kāi)發(fā)中小型可拓展性需求高的Web應(yīng)用程序的利器。很多博客程序,開(kāi)源的cms程序,都是采用CI進(jìn)行的編寫(xiě)。2014-06-06php XMLWriter類(lèi)的簡(jiǎn)單示例代碼(RSS輸出)
這是一段寫(xiě)博客RSS的代碼,需要的朋友可以參考下。2011-09-09百度工程師講PHP函數(shù)的實(shí)現(xiàn)原理及性能分析(三)
這篇文章主要介紹了百度工程師講PHP函數(shù)的實(shí)現(xiàn)原理及性能分析(三),本文講解了常用php函數(shù)實(shí)現(xiàn)及介紹,并作了總結(jié)及建議,需要的朋友可以參考下2015-05-05