-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[6.x] Allow model serialization for typed properties #30604
Conversation
It seems StyleCI is failing because it doesn't supports typed properties yet. |
3d7b61f
to
ea89fe8
Compare
I've ignored the file in StyleCI for now. |
I think need to use __serialize/__unserialize for this. This also added on PHP 7.4 |
@dkulyk that would change the entire narrative of this functionality. The idea behind the current approach is that a fresh instance is retrieved while if we'd serialize everything then that would mean the exact object as is would be serialized. |
@dkulyk also: we'll probably only be able to support that when PHP 7.4 is the new minimum version which won't be before September 2021. |
For PHP 7.3 and below will work old behavior. for 7.4+ new. class A{
public $a = 2;
protected $b = "ss";
private $c = false;
public function __serialize()
{
return [
'a' => $this->a,
'�*�b' => $this->b,
'�A�c' => $this->c
];
}
serialize(new A) -> unserialized on 7.3 and below:
O:1:"A":3:{s:1:"a";i:1;s:4:"�*�b";s:1:"s";s:4:"�A�c";b:1;} //PHP 7.3
O:1:"A":3:{s:1:"a";i:1;s:4:"�*�b";s:1:"s";s:4:"�A�c";b:1;} //PHP 7.4 |
@driesvints Yes, StyleCI parses files as PHP 7.3 right now. Support for PHP 7.4 is likely to land Q1 2020. We haven't decided on a launch date yet for that. |
At the moment it's not possible to use model serialization on jobs with typed properties (which is coming to PHP 7.4). The reason for this is that the property values get replaced with a
ModelIdentifier
instance which contains all the info to unserialize the model or collection later on.This new implementation takes the approach of saving those ModelIdentifiers on a separate
$identifiers
property and replaces the original property values with empty instances of those models or collections. This way, type safety is preserved and we can use typed properties on queueable jobs.This is technically a breaking change because if you expect the properties to contain the
ModelIdentifier
you'll now get an empty model or collection after serialization. But this is also only happening if you're doing anything with the model after serialization and before sending it to the queue, which seems unlikely.Fixes #30494
Small side note: one small other thing that I thought of was if people would be overriding a model constructor with different arguments other than the default that this wouldn't work but I found out that in the current unserialization process the same approach is taken and thus that wouldn't work anyway at the moment. It's also my personal opinion that people shouldn't override a collection or model's constructor but best use named constructors in these cases.