Fatal Error: Class 'Pheanstalk\Pheanstalk` not found

Fatal Error: Class 'Pheanstalk\Pheanstalk` not found

我正在使用一个名为 Pheanstalk 的库,该库是我通过作曲家下载的。我是运行以下脚本:

<?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.

我的 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 版本:https://github.com/pda/pheanstalk/blob/2.1/classes/Pheanstalk/Pheanstalk.php

class 名称是 Pheanstalk_Pheanstalk 而不是 Pheanstalk\Pheanstalk:此时它不符合 PSR-4。

所以你应该只使用:

<?php
use \Pheanstalk_Pheanstalk;

当您在命名空间文件中时。如果不在文件中使用命名空间,则不需要 "import" class.

如果您使用命名空间,反斜杠很重要,因为版本 2.x 中的 class 没有命名空间。

更新

所以你的代码应该是这样的:

<?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

?>

就这些了。