Elektrine lite

← Feed

@heiglandreas@phpc.social

Post #3738743

2026-07-11 11:49 UTC

@nyamsprod@phpc.social For a ValueObject IMO __toString makes (can make) sense. If that is not the case, then the question is whether the object itself should decide upon which kind of string it should represent to. That's IMO too much context to be known by an object. Same for `toArray` - too much context to be known. Serializing an object is not the objects task.

Replies (2)

  • @nyamsprod@phpc.social 2026-07-11 11:59

    @heiglandreas@phpc.social I currently favour using `format()` and `fromFormat()` methods on my objects. with a Format Enum which list the most common use case in *my domain* and expose all the properties so that you can in your own domain define other string representations if needed via a Formatter or a Builder. I would argue that the best trade off I have for now 🤷‍♂️ . But the main take-away is it has to be explicit and __toString is everything but that.

    Open ##3738893

  • @ocramius@mastodon.social 2026-07-11 11:52

    @heiglandreas@phpc.social @nyamsprod@phpc.social obligatory reference: https://github.com/ShittySoft/symfony-live-berlin-2018-doctrine-tutorial/pull/3#issuecomment-460441229

    Open ##4145668