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!!