0

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

?>

The following error appears:

Fatal Error: Class 'Pheanstalk\Pheanstalk' not found in /opt/lampp/htdocs/project_zero/index.php on line 16

with line 16 being: $pheanstalk = new Pheanstalk('127.0.0.1');

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.

The contents of my composer.json file are:

{
  "require": {
    "pda/pheanstalk": "2.1.1"
  }
}

EDITED:

New errors when using:

use \Pheanstalk_Pheanstalk

Errors:

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
Webeng
  • 7,050
  • 4
  • 31
  • 59
  • `composer dump-autoload`? – Arcesilas May 21 '16 at 23:18
  • @Arcesilas do I run that in the terminal and give you the output? I'm relatively new to linux and composer – Webeng May 21 '16 at 23:20
  • Just run it in terminal and retry. – Arcesilas May 21 '16 at 23:20
  • It should be run automatically by composer when installing a new package, though. – Arcesilas May 21 '16 at 23:21
  • @Arcesilas I just ran it in the terminal in the location of the composer.json file, the output was: `Generating autoload files`. I tried runing the script again, same error appeared – Webeng May 21 '16 at 23:22
  • I noticed that some people use: `use Pheanstalk_Pheanstalk` in some online articles, though when I tried switching my `use Pheanstalk\Pheanstalk` with that, I got a different error – Webeng May 21 '16 at 23:23
  • @Arcesilas I have a feeling that it might have something to do with my `composer.json` file and the version or it's format. I updated my question with it's contents. Do you happen to see anything wrong with it? – Webeng May 21 '16 at 23:29
  • The code is PSR-4 compliant, so you actually have to use actual namespace: `use Pheanstalk\Pheanstalk;` – Arcesilas May 21 '16 at 23:34
  • Ah! Version 2.1.1 :) See: https://github.com/pda/pheanstalk/blob/2.1/classes/Pheanstalk/Pheanstalk.php Class name is actually: `class Pheanstalk_Pheanstalk implements Pheanstalk_PheanstalkInterface` – Arcesilas May 21 '16 at 23:36
  • When you use a composer package, don't forget to mention the version... Default behaviour is to consider you use the latest. – Arcesilas May 21 '16 at 23:37
  • @Arcesilas lol sorry i'm newish to classes in php, would I then use: `use Pheanstalk_Pheanstalk implements Pheanstalk_PheanstalkInterface` instead or something similar? – Webeng May 21 '16 at 23:37

1 Answers1

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

The class name is Pheanstalk_Pheanstalk not Pheanstalk\Pheanstalk: it was not PSR-4 compliant at this moment.

So you should just use:

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

Backslash is important if you use namespaces, because the class, in version 2.x was not namespaced.

UPDATE

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

?>

That's all.

Arcesilas
  • 1,388
  • 11
  • 24
  • So I would have to change `user Pheanstalk\Pheanstalk` to `use Pheanstalk_Pheanstalk`? Because I previously mentioned in the comments I tried that and got another error hahah. I'll try again though and post If I get an error – Webeng May 21 '16 at 23:39
  • Edited it again :) – Arcesilas May 21 '16 at 23:41
  • hahah got a new error now, I edited my answer with the errors that I'm getting with the previous modification. I tried using the \ and without the \ also – Webeng May 21 '16 at 23:46
  • As I said: if you don't use namespace, you don't need to import the class. No namespace in your file, no need to use `use Pheanstalk_Pheanstalk`. – Arcesilas May 21 '16 at 23:48
  • You mean instead of using a namespace, to put all the class inside the same file? – Webeng May 21 '16 at 23:51
  • No. I mean: if you use "namespace My\Awesome\Application` then you need to "import" the class with `use \Pheanstalk_Pheanstalk`. If you don't use a namespace, you don't need: just use your class in your code. – Arcesilas May 21 '16 at 23:52
  • I'm completely new to using classes and namespaces lol, sorry if they are stupid questions – Webeng May 21 '16 at 23:52
  • You might consider reading the documentation about [namespaces in PHP](http://php.net/namespace) – Arcesilas May 21 '16 at 23:53
  • ok so the modification to my code that i need for it to work would be to take out `use \Pheanstalk_Pheanstalk` and put in something else? – Webeng May 21 '16 at 23:54
  • Updated it once again, with your example corrected. – Arcesilas May 21 '16 at 23:55
  • Worked! your a genius, thanks man. I'll also read the documentation on namespaces to get up to date. Hopefully I can find out later why the namespace code didn't work. – Webeng May 22 '16 at 00:00
  • I'm not a genius, just a long time user :) Glad I could help! – Arcesilas May 22 '16 at 00:00
  • The namespace does work: with PSR-4 compliant code, which is the case for version 3 of this package. Version 2.1 is only PSR-0 compliant, which does not use namespaces. – Arcesilas May 22 '16 at 00:01
  • Oh so if I understood you correctly, literally in my specific case it was not working because I was using a namespace when the version of the pheanstalk library I had installed wasn't compliant with the necessities to be using namespaces in the first place? – Webeng May 22 '16 at 00:04
  • 1
    That's it. You were using example given for version 3. Switching to branch 2.1 of the code: https://github.com/pda/pheanstalk/tree/2.1 would have given you the correct instructions (which are in your files, in `README.md`) – Arcesilas May 22 '16 at 00:06
  • Nice. Thanks for taking the time to debug the issue. life saver. – Webeng May 22 '16 at 00:08
  • Hey dude, I created another question regarding pheanstalk. If I could have your feedback that would be awesome! http://stackoverflow.com/questions/37370688/pheanstalk-and-beanstalk-functions – Webeng May 22 '16 at 04:57