3

Looked around and can't really find an answer.

I have a web app that is sending SMS messages using the Twilio SDK

Some installs have Twilio installed and some do not.

I want this code to run only if the Twilio files exist.

The regular code is:

require_once ABSPATH.'php/vendor/twilio-php-master/Twilio/autoload.php';
use Twilio\Rest\Client;

I have tried

if(file_exists(ABSPATH.'php/vendor/twilio-php-master/Twilio/autoload.php')) {
    require_once ABSPATH.'php/vendor/twilio-php-master/Twilio/autoload.php';

    use Twilio\Rest\Client;
}

and also

if(file_exists(ABSPATH.'php/vendor/twilio-php-master/Twilio/autoload.php')) {
    require_once ABSPATH.'php/vendor/twilio-php-master/Twilio/autoload.php';
}
if(class_exists(Twilio\Rest\Client)) {
    use Twilio\Rest\Client;
}



if(file_exists(ABSPATH.'php/vendor/twilio-php-master/Twilio/autoload.php')) {
    require_once ABSPATH.'php/vendor/twilio-php-master/Twilio/autoload.php';

}

use Twilio\Rest\Client;

and always get

syntax error, unexpected 'use'

Is there a way to make this conditional?

  • can you show us where this code lives in relation to the class definition? – Nikkorian Dec 16 '22 at 00:33
  • To quote an answer in https://stackoverflow.com/questions/33342994/unexpected-use-t-use-when-trying-to-use-composer, "The "use" keyword is either in front of a class definition to import other classes/interfaces/traits into it's own namespace, or it is inside the class (but not inside a method) to add traits to the class." – Nikkorian Dec 16 '22 at 00:34
  • The class definition is loadd in the autoload file just above – user3404390 Dec 16 '22 at 00:36
  • This is at the top of my file, Its just not all installs wwill have the twilio files, so only want to load this class if the files are there – user3404390 Dec 16 '22 at 00:37
  • the code that loads the class is the require_once() Then if(class_exists(... will tell you whether it's there or not. You don't use 'use' to decide that. You already know. – Nikkorian Dec 16 '22 at 00:45
  • Some of the installs do not have these files. So the require once is conditional.... – user3404390 Dec 16 '22 at 00:46
  • Why don't you just `exit` if the file doesn't exist, then continue normally in the rest of the code? – Barmar Dec 16 '22 at 00:51

2 Answers2

0

Why not use use unconditionally?

 <?php
 use Twilio\Rest\Client;

 if (is_file(ABSPATH.'php/vendor/twilio-php-master/Twilio/autoload.php')) {
      require_once ABSPATH.'php/vendor/twilio-php-master/Twilio/autoload.php';
 }

I can run this code without any issue.

Probably risking name conflicts with Twilio\Rest\Client but I think you'd have this either way.

Marco
  • 7,007
  • 2
  • 19
  • 49
  • Any explanation why putting the use before works and not after? – user3404390 Dec 16 '22 at 01:01
  • @user3404390 I can only quote [php](https://www.php.net/) itself on this: `The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped.` this is what the [doc](https://www.php.net/manual/en/language.namespaces.importing.php) had to say about it. – Marco Dec 16 '22 at 01:05
  • @user3404390 It's a design choice made by the PHP developers. They could've made it work with block scoped `use` keywords I'm sure but there are very good reasons not to do this. You (as an end user) just have to accept this. – Marco Dec 16 '22 at 01:07
  • Thank you so much for taking the time marco-a – user3404390 Dec 16 '22 at 19:13
0

Thank you marco-a

Ended up using

use Twilio\Rest\Client;

 if (file_exists(ABSPATH.'php/vendor/twilio-php-master/Twilio/autoload.php')) {
      require_once ABSPATH.'php/vendor/twilio-php-master/Twilio/autoload.php';
 }

Still not sure why putting the use statement before works, but it does lol

  • `use` appears to be conditional by nature ie. (declaring `use Not/A/Path;` is never "verified"/is inactive until your code calls `new Path` somewhere. There are reasons `use` must generally be "on top" or "outside" its container/area of use, not the least of which is accessibility/ease of management. The including of the file can be done anywhere before the class is initiated, again for ease of management/editability that's probably why it generally is placed along with the relevant `use` statement where it is. – John Smith Dec 16 '22 at 01:23