2015-06-02 34 views
2

我使用composer在更新前同事的Handler類的Cosenary Instagram Class的依賴類後破壞了som遺留代碼。Php在另一個類的構造函數中使用類,並要求使用名稱空間

前者參考包括在處理程序類的類是這樣的:

namespace Acme\Instagram; 
class Handler 
{ 

/* GOT SOME USEFUL VARS HERE */ 

const API_URL = 'https://api.instagram.com/v1'; 

/** 
* Initialize the library 
* 
* @param array $settings Contains our ClientId and upload dir 
* @param string $root_dir The root of our application 
*/ 
public function __construct($settings, $root_dir = __DIR__) 
{ 
    include_once $root_dir . '/vendor/cosenary/instagram/instagram.class.php'; //HERE WAS THE INCLUDE BEFORE 


    // THROW SOME EXCEPTIONS HERE IF SETTINGS MISSING ETC... 

    // New instance of the instagram class 
    $this->_instagram = new \Instagram($this->_clientId); 
} 

/* HANDLE SOM FUNCTIONS HERE */ 

} 

如果我改變include_once到:

require_once $root_dir . '/vendor/cosenary/instagram/src/Instagram.php'; 

然後我得到:

Fatal error: Class 'Acme\Instagram\Instagram' not found in 

我想我需要將它作爲構造函數中的引用傳遞,但這意味着我需要重寫很多代碼d大概還有5-10個其他項目取決於這個Hanlder類。有沒有在這個其他課程中使用instagram課程的方法?

試了遷出include_once和:

use MetzWeb\Instagram\Instagram; 

,但沒有運氣,任何幫助或指針我不勝感激。

回答

1

我不確定您的應用程序的結構如何顯示。 但試試這個:

namespace Acme\Instagram; 

require_once $root_dir.'/vendor/autoload.php'; 

use MetzWeb\Instagram\Instagram AS InstagramClient; 

class Handler 
{ 

/* GOT SOME USEFUL VARS HERE */ 

const API_URL = 'https://api.instagram.com/v1'; 

/** 
* Initialize the library 
* 
* @param array $settings Contains our ClientId and upload dir 
* @param string $root_dir The root of our application 
*/ 
public function __construct($settings, $root_dir = __DIR__) 
{ 
    // New instance of the instagram class 
    $this->_instagram = new InstagramClient($this->_clientId); 
} 

/* HANDLE SOM FUNCTIONS HERE */ 
+0

我得到致命錯誤:命名空間聲明語句必須是在...如果我的命名空間聲明之前使用它的腳本中的第一個聲明。 – Conjak

+0

我再次編輯了代碼檢查。 – num8er

+1

不錯,讓它再次工作。但有一些其他問題。但似乎是另一個問題。謝謝! – Conjak

相關問題