Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable To Call a Class Installed Using Composer

I have installed a PHP wrapper library using Composer. The autoloader seems to be working fine, but I cannot call a class as it says

class 'Diffbot' not found.

I have tried numerous tricks, especially those mentioned in the Composer documentation, but I cannot get it working and I think I must share my problem here.

My composer.json contains the following lines

{
    "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

I am trying to call Diffbot class under Diffbot.php, it contains the following namespaces:

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;

The autoload_psr4.php file under composer/ folder:

// 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'),
);

I am trying to call the Diffbot class from a php script that resides in the same directory as the vendor/ folder in the following manner:

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

Edit

I solved my problem. I just added the following lines. I was confused about PHP namespace.

require_once __DIR__.'/vendor/autoload.php';
$foo = new \Swader\Diffbot\Diffbot('foo');
like image 388
user3668629 Avatar asked Jul 09 '15 08:07

user3668629


1 Answers

Try

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

See PHP doc for reference.

like image 118
Gottlieb Notschnabel Avatar answered Oct 06 '22 23:10

Gottlieb Notschnabel