简体   繁体   English

PSR-4未加载我的项目类

[英]PSR-4 not loading my project classes

I'm using Composer's PSR-4 to autoload classes, but it won't load my classes. 我正在使用Composer的PSR-4自动加载课程,但不会加载我的课程。 Every time I try to use one of my classes the app dies without any error, even though it should display error. 每次我尝试使用我的一个班级时,该应用程序都会死亡,即使它应该显示错误,也不会出现任何错误。

Here is my file structure: 这是我的文件结构:

/
│   composer.lock
│   composer.json
│   index.php
├───src
│       Array.php
│       File.php
├───vendor
        ...

composer.json - autoload part composer.json-自动加载部分

"autoload": {
    "psr-4": {
        "FileManager\\": "src"
    }
}

Start of index.php index.php的开始

<?php

require(__DIR__ . '/vendor/autoload.php');

var_dump(class_exists('Array'), class_exists('Array', false));
var_dump(class_exists('File'), class_exists('File', false));

And it dumps this: 并转储此:

bool(false)
bool(false)
bool(false)
bool(false)

If I add 如果我加

use FileManager\Array;

it will die instantly. 它会立即死亡。 If I add 如果我加

use FileManager\File;

it won't die, but it won't recognize File class either. 它不会死,但也不会识别File类。

I have ran 我跑了

$ composer dumpautoload

Can somebody help me with this? 有人可以帮我吗?

The problem is that you are passing string s to class_exists() . 问题是您要将string s传递给class_exists()

If you use string values to reference class names, you will need to use the fully-qualified class name . 如果使用string值引用类名,则需要使用完全限定的类名

<?php

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

var_dump(
    class_exists('FileManager\ArrayUtil'),
    class_exists('FileManager\File')
);

Alternatively, you can use the class keyword (requires at least PHP 5.5): 另外,您可以使用class关键字(至少需要PHP 5.5):

<?php

use FileManager\ArrayUtil;
use FileManager\File;

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

var_dump(
    class_exists(ArrayUtil::class),
    class_exists(File::class)
);

For reference, see: 供参考,请参阅:

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

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