PHP – Unable to load a class using composer and autoloader

I’m trying to include this library inside my WordPress plugin.

I’ve used this code to include the composer autoloader into the class file but I will always get a 500 error from the server, it seems that the library isn’t loaded when needed

<?php 

/*
* Plugin Name: Custom Ticketing
*/

require_once __DIR__ . '/vendor/autoload.php';

class CustomTicketing {

       public function check_email( WP_REST_Request $request )
    {
        if( !wp_verify_nonce( $request->get_header('X-WP-Nonce'), 'wp_rest' ) ){
            return false;
        } else {
            $email = sanitize_email( $request->get_param('email') );
            $validator = EmailValidation\EmailValidatorFactory::create($email);
            return $validator->getValidationResults->asArray();
        }
    }
}

The error log from wordpress will give me this message and it’s very clear

[13-Mar-2023 14:54:20 UTC] PHP Warning:  Undefined property: EmailValidation\EmailValidator::$getValidationResults in /home/myserver/coolwebsite/wp-content/wp-content/plugins/custom-ticketing/index.php on line 382
[13-Mar-2023 14:54:20 UTC] PHP Fatal error:  Uncaught Error: Call to a member function asArray() on null in /home/myserver/coolwebsite/wp-content/plugins/custom-ticketing/index.php:382

Do I need to require the autoloader inside the class or the method when I need the library to solve the problem??

>Solution :

It looks like you are missing some (). Try this for the "last" line:

return $validator->getValidationResults()->asArray();

Leave a Reply