There's an interesting snippet demonstrated by Taylor Otwell at Laracon EU 2019 that dispatches a few queue jobs. It looks seemingly easy to understand, but, of course, the devil is in the details.
Let's try to dispatch a queue job on our own:
Looks pretty straightforward - a new Post model instance is created and a BroadcastToSubscribers queue job is dispatched.
Let's make our task a bit more complex by specifying a queue connection name:
There are no method calls like finish or enqueue to indicate that we finished the dispatch configuration, so does Laravel know that we call the onConnection method after calling the dispatch method?
We can dump the result of the dispatch method call to see that we have an instance of the Illuminate\Foundation\Bus\PendingDispatch class, which doesn't have any methods to manually finish the queue job dispatch process.
Upon closer inspection, however, we can see that PendingDispatch has the __destruct method, that seemingly does all the heavy lifting.
__destruct is a PHP magic method, that is automatically invoked as soon as all object references are dropped, effectively acting as a destructor.
You may be familiar with destructors already, as they are also used in other languages:
PendingDispatch utilizes the __destruct method to automatically dispatch queue jobs at the end of the current scope:
We can utilize such a technique to write something on our own.
Here is a transaction guard implementation1 that will commit a database transaction as soon as the execution scope reaches its end:
Usage (example source):
In this case TransactionGuard will commit the active database transaction at the end of the request execution scope.
1 - guard terminology is taken from Rust's MutexGuard, RwLock{Read, Write}Guard and similar structs.