2012-11-22 35 views
-1

我已經包含在我的bootstrap.php和實例化的類包括,當我包括bootstrap.php中它給了我一個電話成員函數的非對象變量類要求一旦超出範圍內的功能

bootstrap.php中

require_once "config/constants.php"; 
require_once "libraries/Dropbox/DropboxClient.php"; 

$dropbox = new DropboxClient(array(
'app_key' => DROPBOX_CONSUMER_KEY, 
'app_secret' => DROPBOX_CONSUMER_SECRET, 
'app_full_access' => true, 
),'en'); 

的index.php

require_once "bootstrap.php"; 

dropbox_file_tree(); 

function dropbox_file_tree() 
{ 
    if(!empty($access_token)) { 

    $dropbox->SetAccessToken($access_token); 

    } 

    $files = $dropbox->GetFiles("",true); 

    print_r($files); 
} 
+1

您需要將'$ dropbox'實例傳遞給函數即可。每個功能都有自己的本地範圍;默認情況下爲空。 – mario

回答

1

這無關include,這是一個scope issue,功能僅見局部變量。

您必須使用global關鍵字或$GLOBALS超全局或將其作爲參數傳遞給函數。

+2

絕對必要時才使用全局變量!我不會認爲這種情況是必要的。雖然他可能有一個理由是他在引導文件中聲明瞭Dropbox客戶端,然後嘗試在index.php中訪問它。看起來像糟糕的結構。 – shapeshifter

2

傳遞保管箱對象的功能

dropbox_file_tree($dropbox); 

function dropbox_file_tree($dropbox) 
{ 
    if(!empty($access_token)) { 
     $dropbox->SetAccessToken($access_token); 
    } 

    $files = $dropbox->GetFiles("",true); 

    print_r($files); 
} 
+0

或者更好的是,用您需要的額外功能爲客戶編寫自己的包裝。 – shapeshifter

+0

您需要傳遞'$ access_token'變量... – jeroen

+0

不錯,不是它在示例中的任何地方被聲明過。我認爲OP需要重新考慮他的解決方案。 – shapeshifter