简体   繁体   English

PSR-0自动加载的问题

[英]Issue with PSR-0 autoloading

I am trying to use this composer package with a new project I am working on https://packagist.org/packages/activecollab/activecollab-sdk . 我正在尝试将此组合程序包与正在使用https://packagist.org/packages/activecollab/activecollab-sdk的新项目一起使用。 However when i try and create a new class I keep getting the following errors. 但是,当我尝试创建一个新类时,我不断收到以下错误。

Fatal error: Class 'ActiveCollab\Client' not found

The file that is throwing this error looks like this. 引发此错误的文件如下所示。

require "vendor/autoload.php";

new ActiveCollab\Client;

Which is just being used to test if the files are being loaded in properly. 这只是用来测试文件是否正确加载。 The composer.json of the file which I am trying to use looks like such. 我尝试使用的文件的composer.json看起来像这样。 And I have a feeling the problem is in this file but I can't figure out what. 而且我觉得问题出在此文件中,但我不知道是什么。

stuff...

"autoload": {
      "psr-0": {
        "ActiveCollab\\": "ActiveCollab"
      }
    }

...stuff

包的文件夹结构

Also looking at the autload_namespaces.php file it is being generated as such. 还要查看autload_namespaces.php文件,它是这样生成的。

<?php

// autoload_namespaces.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'ActiveCollab' => array($vendorDir . '/activecollab/activecollab-sdk/ActiveCollab'),
);

I have used psr-0 in some composer packages of my own and everything looks to be right except maybe the camel case in the namespace but i don't see this as being disallowed in the php proposal for psr-0. 我已经在自己的一些composer程序包中使用了psr-0,并且除命名空间中的驼峰式情况外,其他一切看起来都是正确的,但我不认为psr-0的php提案中不允许这样做。

Thanks for any help this has been driving me crazy. 感谢您的帮助,这使我发疯。

The thing is: You cannot simply add a composer.json file with a random autoloading configuration and hope that it works - it actually has to match the naming scheme you are using. 关键是:您不能简单地添加具有随机自动加载配置的composer.json文件,并希望它能工作-实际上必须匹配您使用的命名方案。 That is what this project got wrong, and nobody tested it. 这就是该项目出错的原因,并且没有人对其进行测试。 Which probably means nobody uses this library, and you can expect no support from the creators due to lack of interest. 这可能意味着没有人使用此库,并且由于缺乏兴趣,您可能无法期望创建者提供支持。

But let's see how they react on my pull request to get things back to working again. 但是,让我们看看他们如何响应我的请求 ,使事情重新恢复正常工作。

The composer config looks fine: Is it just the case that you omitted the leading \\ from your class name? 作曲家的配置看起来不错:是否只是在类名中省略了开头的\\的情况?

new \ActiveCollab\Client;

You'll need that if your code is inside another namespace, as it will load it relative to the current namespace. 如果您的代码位于另一个名称空间中,则将需要它,因为它将相对于当前名称空间进行加载。

EDIT: I've just checked out that library, and even with the above fix, the autoloader wasn't quite working. 编辑:我刚刚签出了该库,即使进行了上述修复,自动装带器也无法正常工作。 The autoloader may also be broken due to the composer.json file for the library specifying a PSR0 autoloader, but using ".class.php" extensions (not PSR0 compatible). 由于库的composer.json文件指定了PSR0自动加载器,但使用了“ .class.php”扩展名(与PSR0不兼容),因此自动加载器也可能损坏。 An autoload.php file is included with the library, so if you just require that file, you should be able to use the classes: 该库包含一个autoload.php文件,因此,如果仅require该文件,则应该可以使用这些类:

require 'vendor/activecollab/activecollab-sdk/ActiveCollab/autoload.php';

After doing this, I was able to use the class. 完成此操作后,我就可以使用该类了。

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

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