-1

我的 bootstrap.php 中包含了类,并且在包含的类中实例化了变量,当我包含 bootstrap.php 时,它给了我对非对象上的成员函数的调用

引导程序.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');

索引.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);
}
4

2 回答 2

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);
}
于 2012-11-22T01:51:54.270 回答
1

这与它无关include,这是一个范围问题,函数只能看到局部变量。

您必须使用global关键字或$GLOBALS超全局或将其作为参数传递给函数。

于 2012-11-22T01:45:10.410 回答