Why doesn't Laravel serialize private data members? Is there a technical reason for this or is it a bug?
I had code like this:
abstract class Importer implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
/** The handle of the duty (background task) that this class implements. */
private $duty_id;
...
}
And it kept failing because the value of $duty_id was lost between the initialization in the constructor and the actual use in the handle() method. Having done a few Laravel jobs, I recognized this as a sign of my variable not being serialized. However, it took me a long while to figure out that I had to change private into protected for it to work, something that baffles me yet.
Does PHP not serialize private members or is it a missing flag or check in Laravel itself that causes this behavior?
P.S. I resorted to the Laravel issue list only after nobody answered my post in the Laravel forums.
Change any protected data member in a queue job to private and you'll discover that its value is not persisted between the call to the constructor and the handle() method.
It doesn't. We call it a feature 馃檪
Most helpful comment
It doesn't. We call it a feature 馃檪