致命错误:找不到类'Pheanstalk \ Pheanstalk` [英] Fatal Error: Class 'Pheanstalk\Pheanstalk` not found
问题描述
我正在使用我通过作曲家下载的名为Pheanstalk的库.我正在运行以下脚本:
I am using a library that I downloaded with composer called Pheanstalk. I am running the following script:
<?php
//... some unrelated code
require_once('vendor/autoload.php'); //loading the autoload file from composer
use Pheanstalk\Pheanstalk; //using the namespace
$pheanstalk = new Pheanstalk('127.0.0.1'); //initiating an object
//... some unrelated code
?>
出现以下错误:
Fatal Error: Class 'Pheanstalk\Pheanstalk' not found in /opt/lampp/htdocs/project_zero/index.php on line 16
第16行为: $ pheanstalk = new Pheanstalk('127.0.0.1');
问题:为什么我会出现此错误?上面的脚本基本上是从Pheanstalk github页面上给出的用法示例复制粘贴的: https://github.com/pda/pheanstalk .
Question: Why might I be getting this error? The script above was basically copy-paisted from the Usage Example given on the Pheanstalk github page: https://github.com/pda/pheanstalk.
我的 composer.json
文件的内容为:
{
"require": {
"pda/pheanstalk": "2.1.1"
}
}
已
使用时出现新错误:
use \Pheanstalk_Pheanstalk
错误:
Warning: The use statement with non-compound name 'Pheanstalk_Pheanstalk' has no effect in /opt/lampp/htdocs/project_zero/index.php on line 14
Fatal error: Class 'Pheanstalk' not found in /opt/lampp/htdocs/project_zero/index.php on line 17
推荐答案
根据您的 composer.json
,您使用的是2.1.1版:
According to your composer.json
, you are using version 2.1.1: https://github.com/pda/pheanstalk/blob/2.1/classes/Pheanstalk/Pheanstalk.php
类名称为 Pheanstalk_Pheanstalk
而不是 Pheanstalk \ Pheanstalk
:目前不符合PSR-4.
The class name is Pheanstalk_Pheanstalk
not Pheanstalk\Pheanstalk
: it was not PSR-4 compliant at this moment.
所以您应该使用:
<?php
use \Pheanstalk_Pheanstalk;
当您在命名空间文件中时.如果您不在文件中使用名称空间,则无需导入"该类.
when you're in a namespaced file. If you don't use namespace in a file, you don't need to "import" the class.
如果使用命名空间,则反斜杠非常重要,因为版本2.x中的类未命名空间.
Backslash is important if you use namespaces, because the class, in version 2.x was not namespaced.
更新
所以您的代码应如下所示:
So your code should be like this:
<?php
//... some unrelated code
require_once('vendor/autoload.php'); //loading the autoload file from composer
$pheanstalk = new Pheanstalk_Pheanstalk('127.0.0.1'); //initiating an object
//... some unrelated code
?>
仅此而已.
这篇关于致命错误:找不到类'Pheanstalk \ Pheanstalk`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!