Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

srmoser430's avatar

Using static variable in Job class

I need to retain the value of a PHP resource (a cURL session handle) across executions of a Job class and thought using a static variable within the class would be a great way to do this. However, when I add the static definition to the Job class and attempt to dispatch the Job, I get the following PHP error: "serialize(): 'my_curl_session' returned as member variable from __sleep() but does not exist"

Any thoughts on a workaround are greatly appreciated. Thanks!

0 likes
5 replies
jimmck's avatar

@srmoser430 static class members are not serialized by default, which is happening when the class goes to sleep. You can intervene and store the value in a class member. But saving a cURL reference across instances is not recommended as it is a resource from a system call. Even if it worked sometimes you have no control over when the resource is garbage collected or closed or whatever.

srmoser430's avatar

@usama.ashraf Thanks for the suggestion. I need to share the cURL handle across user sessions so I don't think using the Laravel session will help.

srmoser430's avatar

@jimmck Thanks...not sure I follow your comment "You can intervene and store the value in a class member". I ended up writing my own background job not using the Laravel Job Queue. Mine consists of a daemon that establishes its cURL session and holds onto it (single class instance inside daemon process). The daemon checks its "queue" (i.e., table) for pending requests. This approach also addresses the risk you raise about sharing external resources across class instances.

Thanks....

Please or to participate in this conversation.