I am using the Codebird Twitter SDK for PHP. The code requires you to set an OAuth token to a static variable before getting an instance of the class, e.g.
Codebird::setConsumerKey(a,b); //sets Codebird::_consumer_key and Codebird::_consumer_secret
$cb = Codebird::getInstance();
I run this in a class called TwitterApi
which then passes the $cb
instance around to a few other objects, firstly to a RequestFactory
class, which then passes it to the various TwitterRequests
created by the factory.
Everything works fine, apart from if I create the original TwitterApi
class in the constructor of a Laravel Job, e.g:
public function __construct()
{
$this->twitter = new TwitterApi();
}
public function handle()
{
$this->twitter->doSomething();
}
In this case, the Codebird::_consumer_key
and Codebird::_consumer_secret
static variables are "lost" by the time the $cb
instance reaches the TwitterRequest
class. Weirdly, they are still set at the prior stage inside the RequestFactory
, so they survive two passes but not the third.
Even more weirdly, the following code fixes the problem:
public function handle()
{
$twitter = new TwitterApi();
$twitter->doSomething();
}
The code also works fine in any other context other than when set as a property in the constructor of a Laravel job class. What is going on here? Why does the second code example work, but not the first?
I am assuming it is something to do with the job being serialized and put on the queue, but that doesn't explain why the static variables survive being passed from Class 1 => Class 2 but not Class 2 => Class 3.