The class is not found, even if it is located where I am looking for it

I am trying to load a class for a PHP script that I wrote using class documentation to work.

Since my server is running PHP 5.3, the documentation recommends loading the class as follows:

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 thing that is different is that I included the echo in the set of if and else. Also myUsername is actually my username in the file system path on the server.

Then it (the documentation) assumes that I am making a new instance of the class as follows:

 $elasticaClient = new \Elastica\Client(); 

However, I get an error that the class was not found. Here is exactly what is printed, including the error and the set 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 am trying to make an instance of the class.

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

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

so I don’t understand why it cannot find / load the class.

I really appreciate any help in resolving this issue.

+4
source share
2 answers

not found! - this means that the file was not found. A file, not a class.

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

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

+1
source

Corrected due to excessive error. However, Mike has a link in the notes that are worth keeping.

0
source

Source: https://habr.com/ru/post/1469379/


All Articles