php 未找到 Composer 自动加载类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40126706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:07:19  来源:igfitidea点击:

Composer Autoloading classes not found

phpnamespacescomposer-phpautoload

提问by Ryan Hipkiss

I have folder structure like:

我的文件夹结构如下:

includes/
  libraries/
    Classes/
      Contact/
        Contact.php
        ContactController.php

admin/
  controllers/
    contact/
      edit.php

Contact.php is my class that file that I'm trying to use. The file contains.

Contact.php 是我尝试使用的那个文件的类。该文件包含。

<?php
namespace Classes;

class Contact {
    function __construct() {
        die('here');
    }
}

I have my composer.json file like:

我有我的 composer.json 文件,如:

{
    "autoload": {
        "psr-4": {
            "Classes\": "includes/libraries/Classes/"
        }
    },
}

The file I'm trying to use the Contact class in is edit.phpwithin the admin/controllers/contact/folder. My edit.phpfile is like:

我试图在其中使用 Contact 类edit.phpadmin/controllers/contact/文件位于文件夹中。我的edit.php文件是这样的:

<?php

use Classes\Contact;

$contact = new Contact();

var_dump($contact);

This file has the vendor/autoload.phpfile included, yet I can't seem to get it to use the class?

该文件包含该vendor/autoload.php文件,但我似乎无法使用该类?

回答by Ruslan Osmanov

Classes/Contact/Contact.phpand the composer rule "Classes\\": "includes/libraries/Classes/"imply Classes\Contact\Contactclass, not Classes\Contact.

Classes/Contact/Contact.php并且作曲家规则"Classes\\": "includes/libraries/Classes/"意味着Classes\Contact\Contact类,而不是Classes\Contact.

So if you actually want Classes\Contactclass, move the Classes/Contact/Contact.phpfile up to the parent directory: Classes/Contact.php.

因此,如果您确实需要Classes\Contact类,请将Classes/Contact/Contact.php文件向上移动到父目录:Classes/Contact.php.

If, however, the desired namespace path to the class is Classes\Contact\Contact, then change the use:

但是,如果该类的所需命名空间路径是Classes\Contact\Contact,则更改use

use Classes\Contact\Contact;

And the namespace:

namespace

namespace Classes\Contact;

class Contact {}

Example

例子

├── composer.json
├── includes
│?? └── libraries
│??     └── Classes
│??         └── Contact
│??             └── Contact.php
├── test.php
└── vendor
    ├── autoload.php
    └── composer
        ├── autoload_classmap.php
        ├── autoload_namespaces.php
        ├── autoload_psr4.php
        ├── autoload_real.php
        ├── autoload_static.php
        ├── ClassLoader.php
        ├── installed.json
        └── LICENSE

The files under vendor/are generated by composer.

下面的文件vendor/是由composer生成的。

composer.json

作曲家.json

{
    "name": "testpsr4",
    "autoload": {
        "psr-4": {
            "Classes\": "includes/libraries/Classes"
        }
    }
}

test.php

测试文件

<?php
require_once __DIR__ . '/vendor/autoload.php';

use Classes\Contact\Contact;

$c = new Contact;
$c->test();

includes/libraries/Classes/Contact/Contact.php

包括/库/类/Contact/Contact.php

<?php
namespace Classes\Contact;

class Contact {
    public function test () {
        echo __METHOD__, PHP_EOL;
    }
}

Testing

测试

composer update
php test.php

Output

输出

Classes\Contact\Contact::test