Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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();

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading