Cláudio Ribeiro
Cláudio Ribeiro

Reputation: 1699

Namespace breaking call to Composer imported classes

I'm currently trying to autoload some classes that I created via Composer into some older code I had.

My current folder structure is very simple, just the following:

/src

composer.json

index.php

I have more files, but these are the ones that matter. Then I added the following to both my classes:

namespace ApiReddit;

And my composer.json is the following:

{
    "require": {
            "guzzlehttp/guzzle": "^6.2",
            "adoy/oauth2": "^1.3",
            "twig/twig": "^1.30"
    },
    "autoload": {
      "psr-4": {"ApiReddit\\": "src/"}         
    }

The problem is that now my Searcher.php file that was formerly working has some problems. In the execution of the code I had the following call:

$client = new GuzzleHttp\Client([
       'headers' => ['User-Agent' => 'testing/1.0'],
       'verify' => false]);
}

That now generates the following error:

Fatal error: Class 'ApiReddit\GuzzleHttp\Client' not found in

This means that the using of the new namespace is breaking the calls I had to other composer loaded packages. Any idea on how can I solve this?

Upvotes: 0

Views: 160

Answers (2)

Ihor Burlachenko
Ihor Burlachenko

Reputation: 4905

Looks like the problem is that you are using relative namespace. Try changing: new GuzzleHttp\Client to new \GuzzleHttp\Client. The problem should be solved.

Upvotes: 1

Angad Dubey
Angad Dubey

Reputation: 5452

You can do one of two things:

Either be explicit in importing the class in your Searcher file

use GuzzleHttp\Client;

class Searcher {

...

}

or escape the class when calling directly:

$client = new \GuzzleHttp\Client([
       'headers' => ['User-Agent' => 'testing/1.0'],
       'verify' => false]);
}

Upvotes: 1

Related Questions