简体   繁体   English

PHP命名空间:为什么找不到我的班级?

[英]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. 我已经阅读了一些有关PHP中的名称空间以及compser如何处理名称空间自动加载的知识。 I cannot figure out why my class cannot be found. 我无法弄清楚为什么找不到我的班级。 Can someone help? 有人可以帮忙吗?

I'm using Laravel and here are the relevant bits: 我正在使用Laravel,以下是相关的位:

composer.json relevant content: composer.json相关内容:

"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 : 做一个composer dump-autoload给我在vendor/composer/autoload_namespaces.php的以下行:

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

And my vendor folder structure re: Facebook is: 我的供应商文件夹结构为:Facebook是:

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

Trying $facebook = new Facebook($config) gives me 尝试$facebook = new Facebook($config)给了我

Symfony \\ Component \\ Debug \\ Exception \\ FatalErrorException (E_UNKNOWN) Class 'Facebook' not found Symfony \\组件\\调试\\异常\\ FatalErrorException(E_UNKNOWN)未找到类'Facebook'

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? 您是否已将这些行添加到composer.json并以通常的composer方式安装了库?

"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. Facebook是该类的名称,但Facebook似乎也是您的名称空间的名称。

In that case, you instance it like this: 在这种情况下,您可以像这样实例化它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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