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
Composer Autoloading classes not found
提问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.php
within the admin/controllers/contact/
folder. My edit.php
file is like:
我试图在其中使用 Contact 类edit.php
的admin/controllers/contact/
文件位于文件夹中。我的edit.php
文件是这样的:
<?php
use Classes\Contact;
$contact = new Contact();
var_dump($contact);
This file has the vendor/autoload.php
file included, yet I can't seem to get it to use the class?
该文件包含该vendor/autoload.php
文件,但我似乎无法使用该类?
回答by Ruslan Osmanov
Classes/Contact/Contact.php
and the composer rule "Classes\\": "includes/libraries/Classes/"
imply Classes\Contact\Contact
class, not Classes\Contact
.
Classes/Contact/Contact.php
并且作曲家规则"Classes\\": "includes/libraries/Classes/"
意味着Classes\Contact\Contact
类,而不是Classes\Contact
.
So if you actually want Classes\Contact
class, move the Classes/Contact/Contact.php
file 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