ext-ds icon indicating copy to clipboard operation
ext-ds copied to clipboard

Expand the current __toString behaviour

Open rtheunissen opened this issue 7 years ago • 4 comments

Python example:

a = dict()
a['a'] = 3
a['b'] = 2
a['c'] = 1

print a
{'a': 3, 'c': 1, 'b': 2}

I had this implemented very very early when php-ds was still private, where the structures would echo something similar to JSON. Values that could not be converted to strings had default fallbacks like object(stdClass)#1 and resource(type)#1.

I imagine the above equivalent would echo something like:

{'a': 3, 'b': 2, 'c': 1}

rtheunissen avatar Sep 06 '16 07:09 rtheunissen

Does that mean that __toString() will be a debugging handler? I don't think that matches usual semantics, and we already have var_dump().

nikic avatar Sep 06 '16 08:09 nikic

It's just a different level of verbosity, but you're right, it's not expected behaviour / usual semantics. 😢

rtheunissen avatar Sep 06 '16 09:09 rtheunissen

@nikic to be honest usual semantics of php in this regard is quite inconvenient. Would it be that bad to change it for a more convenient approach by the cost of consistency with the language?

nikita2206 avatar Sep 06 '16 11:09 nikita2206

__toString is either an appropriate representation (think PSR-7 response body) or a debug handler. There is no default handler so I'm not even sure what the "usual semantics of PHP" actually are in this context.

Instead of some custom string representation (mentioned above), what about having all structures just convert to their JSON representation, such that echo $structure is equivalent to echo json_encode($structure). That's both an appropriate representation and a debug handler at the same time.

FWIW, structures currently convert to this format: "object(Ds\Map)"

rtheunissen avatar Sep 07 '16 21:09 rtheunissen