title: Call callback inside Laravel job
body: <p>I have an array with objects. I need to apply my custom callback to every object. Callback is not static, it can be got from somewhere so I can't put it to helpers and make it autoloadable. For every object I create Job.</p>
  <pre class="lang-php prettyprint-override"><code>// let's image we init callback here
  $callback = function($object) {
    // some long operation with object here
  }
  foreach ($array as $object) {
    dispatch(new MyJob($callback, $object));
  }
  // JOB
  class MyJob implements ShouldQueue {
  ...
  __construct(private Closure $callback, private $object) {}
  
  public function handle() {
    call_user_func($this-&gt;callback, $this-&gt;object);
  }
  </code></pre>
  <p>Then I get an Exception: <a href="https://i.stack.imgur.com/WFX0O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WFX0O.png" alt="enter image description here" /></a></p>
  <p>I tried to wrap my callback to</p>
  <ol>
  <li><code>Laravel\SerializableClosure\SerializableClosure</code> - it causes unavailability to serialize some other Closure somewhere else</li>
  <li><code>Opis\Closure\SerializableClosure;</code> - it causes the eternal cycle</li>
  </ol>
  <p>Is there a way I can put my callback inside the job? Or mb there are other ways to apply the callback to objects asynchronously?</p>