自动加载器导致找不到类 [英] Autoloader resulting in class not found
问题描述
我正在尝试在我的wordpress项目中包括一个自动加载器.更具体地说,我正在开发一个包含所有类的Classes/目录的插件.我希望这些类可通过我的wordpress项目根目录和子文件/文件夹的名称空间访问.
I am trying to include an autoloader in my wordpress project. More specifically, I am developing a plugin that contains a Classes/ directory of all my classes. I want these classes to be accessible by namespace to my wordpress project root and children files/folders.
我觉得我的composer.json应该照顾自动加载器的实现,尽管我仍然遇到未发现致命错误的类.还有其他人遇到这个问题吗?感谢您提前提出的建议!
I feel like my composer.json should take care of the autoloader implementation, although I am still getting a Class not found fatal error. Has anyone else run into this issue? I appreciate any suggestions in advance!
这是我到目前为止尝试过的:
This is what I tried so far:
./composer.json
./composer.json
{
"autoload": {
"psr-4": {
"Classes\\": "wp-content/plugins/example-plugin/Classes"
}
},
"require": {}
}
然后...
composer install
./index.php(在wordpress根目录中)
./index.php (in wordpress root)
require_once('./vendor/autoload.php');
$foo = new \Classes\MyService();
var_dump($foo); //Fatal error: Uncaught Error: Class 'Classes\MyService' not found
die();
./wp-content/plugins/example-plugin/Classes/MyService.php
./wp-content/plugins/example-plugin/Classes/MyService.php
namespace Classes;
class MyService {
推荐答案
是的,的确,作曲家应该照顾好您的依赖项和自动加载.您的配置也正确.让我们看一下检查清单:
Yes, indeed, composer should take care of your dependencies and autoloading. Your configuration also is correct. Let's walk through a check list:
您的环境看起来像这样吗?
Does your environment look like this?
$ tree
.
├── composer.json
├── composer.phar
├── index.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
└── wp-content
└── plugins
└── example-plugin
└── Classes
└── MyService.php
您的composer.json看起来像这样吗?
Does your composer.json look like this?
$ cat composer.json
{
"name": "my/project",
"authors": [
{
"name": "My Name",
"email": "my@na.me"
}
],
"autoload": {
"psr-4": {
"Classes\\": "wp-content/plugins/example-plugin/Classes"
}
},
"require": {}
}
您的实现看起来像这样吗?
Does your implementation look like this?
$ cat wp-content/plugins/example-plugin/Classes/MyService.php
<?php
namespace Classes;
class MyService {
}
最后,您的索引看起来像这样吗?
Finally, does your index look like this?
$ cat index.php
<?php
require_once('./vendor/autoload.php');
$foo = new \Classes\MyService;
var_dump($foo);
如果您对所有这些回答都是是",您是否刷新了自动装带器?
If your answer is "Yes" to all of these, have you refreshed your autoloader?
$ composer dump-autoload
如果答案为是",则创建一个新目录,下载composer.phar
,运行php composer.phar init
,将此答案的文件复制并粘贴到该目录中,然后重试.
If your answer is "Yes", then create a new directory, download composer.phar
, run php composer.phar init
, copy and paste this answer's files into that directory, and try again.
如果可行,则diff
将其与您的实现一起使用.我的猜测是,如果到此为止,您将在文件之一中隐藏一个流浪字符-可能是Unicode空白.
If that works, then diff
this with your implementation. My guess is, if you get this far, you have a stray character -- possibly Unicode white space -- hiding in one of your files.
这篇关于自动加载器导致找不到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!