1

I am trying to add the following library (link) to my Symfony project using composer.

I have run

composer require jaggedsoft/php-binance-api

without a problem but I am getting the following error when loading the page.

Attempted to load class "API" from namespace "App\Controller\Binance". Did you forget a "use" statement for another namespace?

public function index(){
   require '../vendor/autoload.php';
   $api = new Binance\API("<api key>","<secret>");
}

Now i am guessing that I need to add a use statement but I am a bit stuck on what it is that I need to add.

8
  • you could either prefix it with \ so new \Binance\Api or use use statements as suggested by php ^^ apparently just adding a use Binance after your namespace might be sufficient. Commented May 5, 2018 at 9:24
  • If I add "use App\Controller\Binance;" I still get the same error message Commented May 5, 2018 at 9:26
  • the point is, your file has the namespace App\Controller so everything not defined otherwise is assumed to live in that namespace. Binance\API for example does not. Commented May 5, 2018 at 9:27
  • The require vendor/autoload statement goes at the very top of the very first file loaded. You would not put it inside a method call. Nothing to do with namespaces or use statements. Commented May 5, 2018 at 14:39
  • @Cerad, I have checked the index.php file and it does already include a require DIR.'/../vendor/autoload.php'; So I have taken that line out but Im still getting Attempted to load class "API" from namespace "App\Controller\Binance". Did you forget a "use" statement for another namespace? Commented May 5, 2018 at 15:00

1 Answer 1

2

To reiterate what I suggested in the comments (options 1 and 3 below):

The namespace of your file - although not explicitly written in your post - is:

App\Controller

without any use statement, the new Binance\API(...) is interpreted as:

App\Controller\Binance\API

which is the concatenation of App\Controller (your namespace) and Binance\API (the classname used).

which of course is not what you want to use, since this is something you tried to include from the binance package. This also explains the error message

Attempted to load class API from namespace App\Controller\Binance. Did you forget a use statement for another namespace?

which is exactly what went wrong. PHP tried to load App\Controller\Binance\API which is class API from namespace App\Controller\Binance.

Now there are A few different ways to fix this:

  1. add use Binance; in the header of your file, then you can use new Binance\API(...)
  2. add use Binance\API; in the header of your file, then you can use new API(...)
  3. don't add a use statement, then you can use new \Binance\APi(...)
  4. add use Binance as Something; in the header of your file, then you can use new Something\API(...); (aliasing the parent namespace Binance as Something may resolve name collisions)
  5. add use Binance\API as BinanceApi; in the header of your file, then you can use new BinanceApi(...);

You decided to use option 1. Which is preferable, if the class (API in this case) isn't very expressive or unique on its own - so is option 5. However, if you use more classes from the Binance namespace, option 1 is preferable.

Option 3 will always work (and might seem preferable if any of the other options seem overkill for some reason) - you can actually get by without any use statement at all, but it can get frustrating to read and write.

Overall, all options are viable and it comes to taste which one to use. Mixing those options may lead to confusion. Inside Symfony I've mostly seen option 2 with the occasional alias (use ... as ...;), especially when using DoctrineORM annotations or when extending some Class which has the same Class name but in a different namespace:

namespace [package1];

use [package2]\[ClassName] as Base[ClassName];

class [ClassName] extends Base[ClassName] { ... }

I hope this explanation helps. The php docs for namespaces are actually helpful, when you understand the core concept of namespaces.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for explaining this, it helps a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.