无法调用使用Composer安装的类

时间:2015-07-09 08:44:23

标签: php composer-php

我使用Composer安装了PHP包装器库。自动加载器似乎工作正常,但我不能按照它说的那样调用类

  找不到'Diffbot'类。

我尝试了很多技巧,特别是那些在Composer文档中提到的技巧,但我无法让它工作,我想我必须在这里分享我的问题。

我的composer.json包含以下行

{
    "require": {
        "swader/diffbot-php-client": "^0.4.4"
    }

}
Directory structure

Vendor
---composer
---guzzlehttp
---react
---swader
---autoload.php

'swader' folder
---diffbot-php-client
    ---src
        ---Abstracts
        ---Api
        ---Entity
        ---Exceptions
        ---Factory
        ---Interfaces
        ---Traits
        ---Diffbot.php

我试图在Diffbot.php下调用Diffbot类,它包含以下命名空间:

namespace Swader\Diffbot;

use Swader\Diffbot\Api\Crawl;
use Swader\Diffbot\Api\Custom;
use Swader\Diffbot\Api\Search;
use Swader\Diffbot\Exceptions\DiffbotException;
use Swader\Diffbot\Api\Product;
use Swader\Diffbot\Api\Image;
use Swader\Diffbot\Api\Analyze;
use Swader\Diffbot\Api\Article;
use Swader\Diffbot\Api\Discussion;
use GuzzleHttp\Client;
use Swader\Diffbot\Factory\Entity;
use Swader\Diffbot\Interfaces\Api;
use Swader\Diffbot\Interfaces\EntityFactory;

/**
 * Class Diffbot
 *
 * The main class for API consumption
 *
 * @package Swader\Diffbot
 */
class Diffbot
{
    /** @var string The API access token */
    protected static $token = null;

autoload_psr4.php文件夹下的composer/文件:

// autoload_psr4.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'Swader\\Diffbot\\' => array($vendorDir . '/swader/diffbot-php-client/src'),
    'React\\Promise\\' => array($vendorDir . '/react/promise/src'),
    'GuzzleHttp\\Stream\\' => array($vendorDir . '/guzzlehttp/streams/src'),
    'GuzzleHttp\\Ring\\' => array($vendorDir . '/guzzlehttp/ringphp/src'),
    'GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'),
);

我试图通过以下方式驻留在与Diffbot文件夹相同的目录中的php脚本来调用vendor/类:

require_once ('vendor/autoload.php');
error_reporting(E_ALL); 
$diffbot = new Diffbot();

修改

我解决了我的问题。我刚刚添加了以下几行。我对PHP名称空间感到困惑。

require_once __DIR__.'/vendor/autoload.php';
$foo = new \Swader\Diffbot\Diffbot('foo');

1 个答案:

答案 0 :(得分:4)

尝试

require_once __DIR__.'/vendor/autoload.php';
use Swader\Diffbot\Diffbot;
$diffbot = new Diffbot();

请参阅PHP doc for reference