Framework: Laravel does not serialize private data members?

Created on 6 May 2018  路  1Comment  路  Source: laravel/framework

Tools

  • Laravel Version: 5.6.*
  • PHP Version: 7.2
  • Database Driver & Version: Percona 5.7 (MySQL compatible)

Description:

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.

Steps To Reproduce:

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.

Most helpful comment

It doesn't. We call it a feature 馃檪

>All comments

It doesn't. We call it a feature 馃檪

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Fuzzyma picture Fuzzyma  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments

CupOfTea696 picture CupOfTea696  路  3Comments

iivanov2 picture iivanov2  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments