Search code examples
phpclassrequireautoloader

Autoloading php classes not found


I'm autoloading classes in my php file, and the autoloaded can locate and require the file with no problem. However, when I then go to use the class in my code, I get the error "Class not found". I don't understand why this is occuring, as the class file can be located and required in the autoloader with no problem. Any help is appreciated

require "../square-php-sdk-master/autoload.php";

$body = new \SquareConnect\Models\CreatePaymentRequest()

Error:

Fatal error: Uncaught Error: Class 'SquareConnect\Models\CreatePaymentRequest' not found in C:\wamp64\www\testSite.test\include\order-cart.inc.php

Solution

  • Check the namespace of the files being loaded by the autoloaded. Clearly, the autoloader is able to find them, but they are loaded into a namespace other than SquareConnect\Models...
    You'll find the namespace at the top of the loaded file, make sure it say:

    namespace SquareConnect\Models
    

    In lots of the Square APIs, they use the namespace \Square and not \SquareConnect so that is something to check too