2014-09-03 38 views
1

我發現自動加載該頁面PhalconPHP:http://docs.phalconphp.com/en/latest/reference/loader.htmlPhalconPHP:如何自動加載

我明白我需要做的設置裝載機。不過,我的問題是,現在我有一個$裝載機變......我該怎麼辦呢?有什麼我需要附加它?我的猜測是,它添加到$迪,但$迪似乎並不有一個「裝載機」鍵。

// Creates the autoloader 
$loader = new \Phalcon\Loader(); 

//Register some namespaces 
$loader->registerNamespaces(
    array(
     "Example\Base" => "vendor/example/base/", 
     "Example\Adapter" => "vendor/example/adapter/", 
     "Example"   => "vendor/example/", 
    ) 
); 

// register autoloader 
$loader->register(); 

// *** What goes here? 

謝謝!

+0

你可能只有''「例子」=>「vendor/example /」''btw。 – JCM 2015-10-08 15:56:27

回答

3
// Creates the autoloader 
$loader = new \Phalcon\Loader(); 

//Register some namespaces 
$loader->registerNamespaces(
    array(
     "Example\Base" => "vendor/example/base/", 
     "Example\Adapter" => "vendor/example/adapter/", 
     "Example"   => "vendor/example/", 
    ) 
); 

// register autoloader 
$loader->register(); 

$var = new Example\Base(); 

指定的名稱空間和類將在您要使用它們時自動加載。

+0

太棒了。應該剛剛嘗試過。謝謝:) – 2014-09-03 19:00:39

4

另外不要忘記,如果你使用的是composer,如果它遵循PSR-0,那麼堅持使用它的自動加載器來處理任何供應商特定的軟件包以及你自己的代碼總是更容易(通常更好)。

# composer.json 
{ 

    "require": { 
     "…": "…" 
    }, 
    "autoload": { 
     "psr-0": { 
      "": "../src" 
     } 
    } 
} 

// Include composer's autoloader in your index.php. 

include __DIR__ . '../vendor/autoload.php'; 
+1

我會用這個除了爾康的自動加載? – 2014-09-04 21:39:30

+1

號你要處理的另一個自動加載,如果你使用的是外部庫,但不使用作曲的唯一原因 - 你沒有選擇的話,除非他們有自己的自動加載(和他們幾個人真正做到) 。作曲家確實與管理軟件包和自動加載一個踢屁股的工作,所以在另一個自動加載到應用程序堵漏中,就像將第五站了一隻狗。 – 2014-09-04 22:37:32