Jump to content

How to Use External Library in PW Module? (SOLVED)


EyeDentify
 Share

Recommended Posts

Hello There Guys.

I am in the process of getting into making my first modules for PW and i had a question for you PHP and PW gurus in here.

I was wondering how i could use an external library, lets say TwitterOAuth in my PW module.

Link to library
https://twitteroauth.com/

Would the code below be correct or how would i go about this:

<?PHP
namespace ProcessWire;

/* load the TwitterOAuth library from my Module folder */
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;

class EyeTwitter extends WireData,TwitterOAuth implements Module {
  
  /* vars */
  protected $twConnection;
  
  /* 
  	extend parent TwitterOAuth contructor
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret);
  */
  public function myTwitterConnection ($consumer_key, $consumer_secret, $access_token, $access_token_secret) {
    
    /* save the connection for use later */
    $this->twConnection = TwitterOAuth::__construct($consumer_key, $consumer_secret, $access_token, $access_token_secret);
    
  }

  
}

?>

Am i on the right trail here or i am barking up the wrong tree?

I don´t need a complete solution, i just wonder if i am including the external library the right way.
If not, then give me a few hint´s and i will figure it out.

Thanks a bunch.

/EyeDentify

Edited by EyeDentify
Added Solved to title
  • Like 1
Link to comment
Share on other sites

Your method seems fine to me.

Something like Composer would be another option, but then you'd have to explain to your module's intended users how to use it, and expect them to have it installed, before they can get started with your module. Depending on the use case that may or may not be worth the hassle.

In RedBeanPHP I've bundled the RedBean ORM into a module, and pretty much the only difference is that I'm loading the file and instantiating the class in the init() method – but even then I'm mainly doing it that way because I wanted to provide a config setting for selecting which version of the library to include.

  • Like 3
Link to comment
Share on other sites

2 hours ago, EyeDentify said:

<?PHP namespace ProcessWire; /* load the TwitterOAuth library from my Module folder */ require "twitteroauth/autoload.php"; use Abraham\TwitterOAuth\TwitterOAuth; class EyeTwitter extends WireData,TwitterOAuth implements Module {

The only thing what may occure in very rare cases is, that another module in the same PW installation also uses the TwitterOAuth lib, stored in a different place. To avoid conflicts, one can add a conditional check before including it, using the PHP function class_exists().

<?PHP
namespace ProcessWire;

if(!class_exists("Abraham\TwitterOAuth\TwitterOAuth") {
    /* load the TwitterOAuth library from my Module folder */
    require_once("./twitteroauth/autoload.php");
}
use Abraham\TwitterOAuth\TwitterOAuth;

Also one may prefer require_once() over require(), if there is a chance that the class / class file is loaded twice.

 

  • Like 3
Link to comment
Share on other sites

16 minutes ago, horst said:

Also one may prefer require_once() over require(), if there is a chance that the class / class file is loaded twice.

... except if you're already checking with class_exists(), in which case it's just a tiny but unnecessary overhead ?

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...