简体   繁体   中英

PHP Namespaces: why isn't my class found?

I've read a bit about namespaces in PHP and how compser handles the autoloading of namespaces. I cannot figure out why my class cannot be found. Can someone help?

I'm using Laravel and here are the relevant bits:

composer.json relevant content:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/libraries"
    ],
    "psr-0": {
        "Facebook\\":"vendor/facebook/php-sdk-v4/src/"
    }
}

doing a composer dump-autoload gives me the following line in vendor/composer/autoload_namespaces.php :

'Facebook\\\\' => array($vendorDir . '/facebook/php-sdk-v4/src'),

And my vendor folder structure re: Facebook is:

vendor
   |__ facebook
         |__ php-sdk-v4
                  |__ src
                       |__ Facebook
                              |__ ..
                              |__ Facebook.php
                              |__ ..

Trying $facebook = new Facebook($config) gives me

Symfony \\ Component \\ Debug \\ Exception \\ FatalErrorException (E_UNKNOWN) Class 'Facebook' not found

What am I doing wrong?!

Anything from the vendor folder should be loaded automatically so there is no point for using these lines

"psr-0": {
    "Facebook\\":"vendor/facebook/php-sdk-v4/src/"
}

Have you added these lines to your composer.json and installed the library the usual composer way?

"require": {
    "facebook/php-sdk-v4": "~5.0"
}

Facebook is the name of the class, but Facebook seems to also be the name of your namespace.

In that case, you instance it like this:

$facebook = new \Facebook\Facebook($config);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM