2013-01-03 32 views
1

當我運行test.php時,爲什麼總是出現錯誤!class_exists行?!class_exists在magento中不起作用?

這是test.php的:

<?php //test.php 
    require_once './app/Mage.php'; 
    Mage::app()->setCurrentStore(0); 
    Mage::setIsDeveloperMode(true); 


    require_once("test-class.php"); 
?> 

這是測試class.php:

<?php //test-class.php 
     if (!class_exists("AClass")) { 
      class AClass { 
       public function AnAction() { 
        return 123; 
       } 
      } 
     } 
?> 

回答

3

因爲Magento的引導應用程序/ Mage.phpregisters an autoloader,您的來電class_exists()觸發器試圖爲這個類加載一個類定義。這種行爲可以通過傳遞false改變:

<?php //test-class.php 
    if (!class_exists("AClass",false)) { 
     class AClass { 
      public function AnAction() { 
       return 123; 
      } 
     } 
    } 
?> 

此外,引導建立了包括道路論據由自動使用:

$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'; 

配售類定義上述任何目錄將允許它需要在需要定義時進行定義。