[英]PSR-4 autoloading not working - cannot find class
I'm trying to split up a long file into smaller chunks, so I created an src
folder, and am trying to reference it from the main Extension.php
file (which loads and works fine, by the way). 我试图将一个长文件分成较小的块,所以我创建了一个src
文件夹,并试图从Extension.php
主文件中引用它(顺便提一下,它可以正常工作)。
So, I add the src
folder to the psr-4 autoloading array: 因此,我将src
文件夹添加到psr-4自动加载数组中:
"psr-4": {
"Bolt\\Extension\\AndyJessop\\SurveyMonkey\\": [
"",
"src/"
]
}
I create the Test.php
file inside src
: 我在src
创建Test.php
文件:
<?php
namespace Bolt\Extension\AndyJessop\SurveyMonkey;
class Test
{
public function test() {
return 'success';
}
}
In the Extension.php
file (which is under the same namespace), I have this function that is called: 在Extension.php
文件(位于同一名称空间下)中,我具有以下功能:
use Bolt\Extension\AndyJessop\SurveyMonkey\Test;
public function testing(){
return Test::test();
}
But I get the following error: 但是我收到以下错误:
Error: Class 'Bolt\Extension\AndyJessop\SurveyMonkey\Test' not found
File: extensions/local/andyjessop/surveymonkey/Extension.php
First, either run composer update
or composer dump-autoload
to generate the autoload system. 首先,运行composer update
或composer dump-autoload
生成自动加载系统。
Next, make sure that you include (require_once is preferable) the autoload at the top of your entrypoint(s): 接下来,确保在入口点的顶部包括(最好是require_once)自动加载:
require_once __DIR__ . '/path/to/vendor/autoload.php';
NB: if you have PHP 5.3 or lower, replace __DIR__
with dirname(__FILE__)
. 注意:如果您的PHP 5.3或更低版本,请将__DIR__
替换为dirname(__FILE__)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.