无法从单独的文件加载 Twitteroauth class

Can't load Twitteroauth class from a separate file

在我的应用程序中使用非常旧的 TwitterOAuth 库已有多年(运行良好)并决定升级。请注意,新版本现在包含 autoload.php,它会从 /src 目录自动加载许多文件 (类)。按照说明做了一切,但自动加载器不工作。在互联网上搜索并调试应用程序 3 个多小时后,我发现只有将所有内容(自动加载器、函数等)组合到一个文件中时,TwitterOAuth 才能工作。如果我在 functions.php 中保留功能,在 settings.php 中保留设置,等等,则没有任何效果。

如何解决它,以便我可以像旧版本一样对多个文件使用 TwitterOAuth?为了大家方便,我做了一个测试文件,大家可以看看问题所在- http://www.lipskas.com/test.zip

只需 download/launch 它在您的 server/localhost 上。如果你打开 index.php 文件,你会得到 'Class 'TwitterOAuth' not found' 的错误,因为所有的代码都被分成了 3 个文件(index.php, settings.php, functions.php).

现在打开 works.php,您将看到一切正常,因为将来自 3 个文件的完全相同的代码复制到一个文件中。什么鬼?

更新:这是来自 works.php 的代码,这个有效:

<?php
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;

function do_something_with_twitter()
{
$connection = new TwitterOAuth("aaaa", "bbbb");
$content = $connection->get("account/verify_credentials");
print_r($content);
}

do_something_with_twitter();
?>

这是来自 3 个独立文件的代码,它不起作用:

index.php

<?php
require "settings.php";

do_something_with_twitter();
?>

settings.php

<?php
require "functions.php";
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
?>

functions.php

<?php
function do_something_with_twitter()
{
$connection = new TwitterOAuth("aaaa", "bbbb");
$content = $connection->get("account/verify_credentials");
print_r($content);
}
?>

如果有人有同样的问题,你可以这样解决:

使用新版本的 Twitteroauth 库,你应该在每个需要调用 Twitter 库的文件中包含这一行(如果你的应用程序有多个文件):

use Abraham\TwitterOAuth\TwitterOAuth;