致命错误:找不到类' Pheanstalk \ Pheanstalk`

时间:2016-05-21 23:15:38

标签: php composer-php pheanstalk

我正在使用我用作曲家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

1 个答案:

答案 0 :(得分:1)

根据您的composer.json,您使用的是版本2.1.1:https://github.com/pda/pheanstalk/blob/2.1/classes/Pheanstalk/Pheanstalk.php

班级名称为Pheanstalk_Pheanstalk而不是Pheanstalk\Pheanstalk:此时此版本不符合PSR-4标准。

所以你应该使用:

<?php
use \Pheanstalk_Pheanstalk;

当你在命名空间文件中时。如果您不在文件中使用命名空间,则无需“导入”该类。

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

<强>更新

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

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

?>

就是这样。