Magento 是如何實(shí)現(xiàn)自動加載類文件的
在magento中, 除了控制器的類文件,一般都會自動加載類,在平常的PHP中,我們只要定義__autoload魔術(shù)方法就可以讓PHP根據(jù)__autoload方法定義 的規(guī)則去尋找類,但是magento里,方法的調(diào)用,都對象劃了,所以他在定義__autoload時有點(diǎn)不同,magento調(diào)用了PHP的 spl_autoload_register方法來注冊__autoload,PHP的說明文檔是這樣說的:宇塵網(wǎng)絡(luò)幫助中心域名知識|虛擬主機(jī)知識|VPS主機(jī)知識|服務(wù)器知識|網(wǎng)站建設(shè)知識|網(wǎng)絡(luò)推廣
將函數(shù)注冊到SPL __autoload函數(shù)棧中。如果該棧中的函數(shù)尚未激活,則激活它們。
如果在你的程序中已經(jīng)實(shí)現(xiàn)了__autoload函數(shù),它必須顯式注冊到__autoload棧中。因?yàn)?/FONT>
spl_autoload_register()函數(shù)會將Zend Engine中的__autoload函數(shù)取代為spl_autoload()或
spl_autoload_call()仿牌空間、香港月付空間、韓國月付空間、日本月付空間、荷蘭VPS、香港VPS
Magento實(shí)現(xiàn)自動加載類,還調(diào)用了一個PHP的方法set_include_path,在文檔中,PHP是這樣解析的:Sets the include_path configuration option,
也就是說,設(shè)置包含路徑。
Magento先在Mage.php的文件中,定義set_include_path,代碼如下:
$paths[] = BP . DS . ‘app’ . DS . ‘code’ . DS . ‘local’;
$paths[] = BP . DS . ‘app’ . DS . ‘code’ . DS . ‘community’;
$paths[] = BP . DS . ‘app’ . DS . ‘code’ . DS . ‘core’;
$paths[] = BP . DS . ‘lib’;
$appPath = implode(PS, $paths);
set_include_path($appPath . PS . Mage::registry(‘original_include_path’));
include_once “Mage/Core/functions.php”;
include_once “Varien/Autoload.php”;
先告訴PHP編譯器,這些路徑都是默認(rèn)包含進(jìn)來的,也就是__autoload會自動在這些地方尋找類。美國月付空間、月付美國空間、美國VPS、美國服務(wù)器租用
然后,magento再調(diào)用Varien_Autoload類的register方法進(jìn)行注冊__autoload.
至于注冊規(guī)則,則要看Varien_Autoload類的autoload方法了,他通過str_replace(‘ ‘, DIRECTORY_SEPARATOR, ucwords(str_replace(‘_’, ‘ ‘, $class))),對magento類的下劃線和路徑分隔符進(jìn)行轉(zhuǎn)意。
Magento的自動加載類,其實(shí)也就那么簡單。。。
更多技術(shù)知識,更多網(wǎng)絡(luò)服務(wù),盡在宇塵網(wǎng)絡(luò)。我們的成長,離不開你們的支持,宇塵網(wǎng)絡(luò),歡迎您的訪問。