Make a quick vote 🗳 ️How do you refer to SuiteCRM extensions: add-ons or plug-ins? Click here to have your say 👈

Newby Question - Autoload/Use statement in Class - OOP Basics

Sorry to ask a newby question like this…I’m sure this is silly…but:

Having difficulty with a ‘use’ statement “inside” a class…not sure how to implement.

I’m building a LogicHook to query the Twilio API to ‘validate’ that a given phone number is a “Mobile” number.

I’m using the Twilio APK…with used composer to install, and has an autoload file.

In the sample code provided by Twilio (which DOES work ‘outside’ of a Class), it includes some files, and uses a ‘use’ statement like this.

// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once __DIR__ . '/../../../AMS/vendor/autoload.php';
require_once __DIR__ . '/../../../AMS/config.php';
use Twilio\Rest\Client;

However, obviously (even to me) you can’t access the ‘required’ file, and it’s variables etc, “inside” a class in a class file.

If I include the “requires” INSIDE the class definition, all is well, and I have access to all the variables I need from my ‘config’ file…but the “Use” statement causes an error if I put ‘it’ inside the class.

This ERRORS:

class accountsMobilePhoneValidation
{
    public function validateMobileNumber($bean, $event, $arguments)
    {
        require_once __DIR__ . '/../../../AMS/vendor/autoload.php';
        require_once __DIR__ . '/../../../AMS/config.php';
        use Twilio\Rest\Client;  // <- this line causes the error......

    }
}

Any advice???
Again…sorry for the n00b question.

Thanks all!!!

Have you tried putting all the requires and the use right on the top of your hook file, immediately after the <?php opening tag?

I did…and the variables that are included in the config files (etc) are then not accessible in the class function.
Again…I could be doing something REALLY dumb, but that was the catalist for the question…my inability to access the Config.php variables inside the class function…so I then moved the Require statements “inside” the class, and I can now access them…“Except” the “use” statement seems “not” to work inside the class.

Am I even describing this correctly?? :grin:
Thanks for helping!!

It’s hard to advise you without knowing what is in those files.

But pay attention to php_errors.log, see if the requires are generating FATAL errors (if the paths are wrong).

If not, and those loaded files contain classes, you should have no problems in accessing them from inside your own classes. You need to instantiate the class with a new statement before going into any class members or functions.

If you can provide (or point to) the contents of that config.php, that will help me se what is going on

I sincerely appreciate the feedback…I was able to solve my issue.
Had to put the “using” statement outside the function, and the require statement “inside” the function.
I’m not sure if that’s ideal, but works.

Thanks again!!