4

I am trying to load a class in order for a PHP script that I wrote, with help of the classes' documentation, to work.

Since my server is running PHP 5.3, the documentation recommended loading the class like this:

spl_autoload_register(function($class){
    if (file_exists('/webgit/webroot/home/myUsername/www/elastica/lib/' . $class . '.php')) {
       echo " found \n ";
       require_once('/webgit/webroot/home/myUsername/www/elastica/lib/' . $class . '.php');
    }
    else {
       echo " not found! ";
    }
});

The only things that are different are that I included echo's in the suite of the if and else. Also myUsername is actually my username in the file-system path on the server.

Then, it ( the documentation ) suggests that I make a new instance of the class like so:

$elasticaClient = new \Elastica\Client();

However, I am getting an error that the class is not found. Here is exactly what is printed including the error and the suite of my if-else:

not found! Fatal error: Class 'Elastica\Client' not found in /webgit/webroot/home/myUsername/www/elastica/index.php on line 17

line 17 is the line where I try to make an instance of the class.

now, index.php is where the above code is and it is located, on my server, in /webgit/webroot/home/myUsername/www/elastica/

and all the class files are in /webgit/webroot/home/myUsername/www/elastica/lib/

so, I don't understand why it is not able to find / load the class.

I would greatly appreciate any help in solving this!

IMUXIxD
  • 1,223
  • 5
  • 23
  • 44
  • What is the full path name of the file that holds your class (you haven't told us the name of the file.) – Mike Brant Mar 14 '13 at 23:37
  • @MikeBrant I just realised class is never defined. For this part of the code I just wrote what the documentation had as an example. But, to answer your question, there seems to be multiple files in the lib dir that resemble classes. – IMUXIxD Mar 14 '13 at 23:52
  • So is this an invalid question then? If the class isn't defined why would you expect it to load? – Mike Brant Mar 14 '13 at 23:55
  • @MikeBrant no, the class is defined. The variable $class has no value. The documentation never said to give it a value. It said the first code in an example is to have the first block of code in my question and then the second block of code right after. – IMUXIxD Mar 14 '13 at 23:59

2 Answers2

1

not found! - it's mean that the file was not found. File, not class.

Variable $class contains full class name with namespaces Elastica\Client. Are you sure that you have /webgit/webroot/home/myUsername/www/elastica/lib/Elastica\Client.php file?

Check How do I use PHP namespaces with autoload? and learn about PSR-0.

Community
  • 1
  • 1
sectus
  • 15,605
  • 5
  • 55
  • 97
  • ah, that is correct that it means the file was not found. But I do have a client.php in /webgit/webroot/home/myUsername/www/elastica/lib/Elastica/ Why is the last slash a backward-slash in your filesystem path? – IMUXIxD Mar 15 '13 at 01:24
  • Edited answer. `$class == 'Elastica\Client'`. After concatination filename will be `/webgit/webroot/home/myUsername/www/elastica/lib/Elastica\Client.php`. Don't forget that linux file system is case sensitive. – sectus Mar 15 '13 at 01:30
  • Ah, well I don't know if this makes a difference, but Client.php is located here: /webgit/webroot/home/myUsername/www/elastica/lib/Elastica/Client.php and this is a linux server, but the path to that file seems to be the same as what it is looking for... – IMUXIxD Mar 15 '13 at 01:35
  • 1
    You must replace `\ to /`. `$class = str_replace('\\', '/', $class);` – sectus Mar 15 '13 at 01:41
  • Where should I add that line? Inside the spl_autoload_register function right before it closes? Before that function? Or after it right before the instance of the class is made? Or right before it does the file_exists ? – IMUXIxD Mar 15 '13 at 02:09
  • I put the str_replace where it made most sense to me right before the if(file_exists... and inside the spl_autoload_register()'s suite. – IMUXIxD Mar 15 '13 at 02:16
0

Redacted due to excessive wrongness. Mike has a link in the comments worth preserving, however.

Jerry
  • 3,391
  • 1
  • 19
  • 28
  • 1
    The backslash is a PHP namespacing syntax. http://www.php.net/manual/en/language.namespaces.rationale.php – Mike Brant Mar 14 '13 at 23:54
  • 1
    I have never seen \ in a class name either. That is why I was confused by it. Yes, with the default set up of the files that I got from GitHub, the class is in multiple files in the lib directory. 1.) Ah, I didn't even think about that. Good catch! 2.) I don't full understand what you mean by issue number 2. Yes, there is a Client.php in the lib/Elastica/ dir. I will try this and let you know. – IMUXIxD Mar 14 '13 at 23:55
  • 1
    @MikeBrant Yes, the documentation called it namespacing. – IMUXIxD Mar 14 '13 at 23:57
  • I am still getting that error after removing the first back-slash and changing the second one to a forward-slash. – IMUXIxD Mar 15 '13 at 00:00
  • Yeah, after reading up about namespaces at @Mike's link above, I realize I was pretty much wrong. I'd delete my answer, but I think his link is valuable, So I'm just going to redact most of what I said. – Jerry Mar 15 '13 at 00:06