-
Notifications
You must be signed in to change notification settings - Fork 22
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
Message class subclass cannot be serialized #68
Comments
@karelvasicek See comments raise by @SamMousa: c656520#commitcomment-137621946 Why do you want to extend the class? Can you provide a use case of where extending class is the best way? |
All properties have getters right? You could implement a serializer that uses those. |
I posted an example in #58 (comment) . We extend the |
I might be missing something, but Anyhow, after a new (important) I wonder what's wrong with |
It makes them part of the public API. So any change to them then becomes a break in backwards compatibility. |
Is the recently added |
As a hack you can copy the two properties and Permanent solution would be not to abuse the class. For example taking a second look at your example I wonder why do you need |
I agree; it's not a good thing to add at all. Unfortunately I cant follow every commit in every repo where I contribute; so bad things are bound to happen. In my opinion you should not be serializing objects you do not own. What happens if any of the libraries get updated while serialized in the queue? Serialization is risky and with Yii2 comes with extra caveats because the message object also has a reference to the mailer component which cannot / should not be serialized; upon deserialization it tries to "magically" retrieve the mailer from the application, but your console application might be configured differently. |
It prevents library developers from changing or removing the fields without breaking things since someone might have overriden. But if they are private then you are sure that no one is affected if you decided to remove it and replace with a different design |
Thanks guys!
Different approaches could be used, of course, but extending a class and putting into the queue does not look like an abuse to me.
It was a simple example. We use more properties. But as for the |
Well, that sounds like, again "abusing" this class for what it should not do. The class is for Message. I suggest you serialize the class then use some ways to add metadata. One example would be storing JSON version, with or other similar ways, which will avoid changing the class internals while doing the job |
It is. You're misusing fundamental OO concepts. You should not be extending 3rd party classes (I realize the irony since Yii2 often forces you to extend it's base classes). Use composition over inheritance. Instead of trying to make some other object do what you need, why not create a new DTO (Data Transfer Object) that does just what you need: final readonly class PdfInvoiceMail {
public function __construct(
public string $toEmail,
public string $body,
public int $invoiceId
);
} Put that in the queue, as you can see your frontend code doesn't even need to know what kind of mailer implementation you use. |
Closing since what @SamMousa proposed makes perfect sense to me. @karelvasicek please check if the approach is good for you. |
Related to #58 and #59
properties are
private
again so subclasses cannot be serialized using currentmaster
.The text was updated successfully, but these errors were encountered: