PHP7.4 only | Unhandled "Uninitialized" in serialization #33637
-
Description:The with PHP7.4 introduced Typed Properties V2 have also added a new state.
https://wiki.php.net/rfc/typed_properties_v2#uninitialized_and_unset_properties If a typed property, which is initalized in the
Steps To Reproduce:
class ProcessPodcast implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
// this property can be serialized, as it is initalized in the constructor (before serialize-deserialize)
protected Podcast $podcast;
// but when it tries to serialize this property to push the job to the queue,
// you will get the exception, as the handle-method has not run yet
protected AudioProcessor $processor;
public function __construct(Podcast $podcast)
{
$this->podcast = $podcast;
}
public function handle(AudioProcessor $processor)
{
$this->processor = $processor;
// here's some very complex stuff which uses the processor in various methods, that's why it's made accessible as class-property.
}
} Suggested Solution:In PHP7.4, the corresponding method ReflectionProperty::isInitialized was introduced. So for >= 7.4 we can just check this before accessing the the the property, for example in SerializesModels:129. But I do not yet know how to resolve it in a backwards compatible way. Workaround:Anyone finding this and in need of a workaround: protected ?AudioProcessor $processor = null; |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
We only support model serialization in jobs sorry. I'll move this to discussions as a feature request. Feel free to attempt a PR if you like. Thanks. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the quick response. When I find some time I'll try to work out a PR with a backwards-compatible fix for this. |
Beta Was this translation helpful? Give feedback.
-
This problem will be fixed in the releases on Tuesday. |
Beta Was this translation helpful? Give feedback.
This problem will be fixed in the releases on Tuesday.