PHP-找不到类,但它是必需的


PHP - Class not found, but is required

我使用的是Abraham TwitterOAuth PHP SDK。我在index.php的顶部包含了库(文件存在+我使用<?php)。

<?php
require 'inc/oauth/autoload.php';
var_dump(file_exists('inc/oauth/autoload.php')); //true
use Abraham'TwitterOAuth'TwitterOAuth;

下面,在HTML之间,我在div中包含一个.php文件,如下所示:

<?php include('div.php'); ?>

在这个div.php文件中,我实例化了TwitterOAuth类,它抛出了一个错误:

if (isset($_COOKIE['token']) && !empty($_COOKIE['token'])) {
        $token = objectToArray(json_decode($_COOKIE['token']));
        $connection = new TwitterOAuth(CONSUMER, SECRET, $token['oauth_token'], $token['oauth_token_secret']);

这会引发错误Class 'TwitterOAuth' not found

注意

在我将代码移动到div.php之前,这是正常的。然而,我需要将其分离,因为将来我将使用JQuery刷新div。

该类使用Abraham'TwitterOAuth命名空间,因此必须包含它们或使用fqn(完全限定类名)

    $connection = new 'Abraham'TwitterOAuth'TwitterOAuth(CONSUMER, SECRET, $token['oauth_token'], $token['oauth_token_secret']);