Array to String Conversion

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2404 Views - Last Post: 13 May 2015 - 06:06 AM

#1 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 4129
  • View blog
  • Posts: 13,023
  • Joined: 08-June 10

Array to String Conversion

Posted 10 April 2015 - 07:26 AM

Hi,

while coding for my recent project, I came to wonder if there can be a sensible array (-object) to string conversion or if it makes more sense to throw an exception (rather than to let it emit a catchable fatal error).
Is This A Good Question/Topic? 1
  • +

Replies To: Array to String Conversion

#2 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6544
  • View blog
  • Posts: 30,653
  • Joined: 10-May 07

Re: Array to String Conversion

Posted 10 April 2015 - 08:11 AM

Why not use sprintf?
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 4129
  • View blog
  • Posts: 13,023
  • Joined: 08-June 10

Re: Array to String Conversion

Posted 10 April 2015 - 08:17 AM

I think you misunderstood the problem. itís not about printing array data via functions, but about calling the array in a print context.

ex.
$ex = ['foo', 'bar'];

// lots of lines later

echo "some arbitrary $ex text";



For arrays PHP would print the word "Array" and emit a warning. but what should an object wrapping an array do?
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1790
  • View blog
  • Posts: 5,705
  • Joined: 15-January 14

Re: Array to String Conversion

Posted 10 April 2015 - 09:36 AM

Are you talking about using the object to provide a method to print the array if the object gets converted to a string?
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 4129
  • View blog
  • Posts: 13,023
  • Joined: 08-June 10

Re: Array to String Conversion

Posted 10 April 2015 - 09:41 AM

in a way yes.

on the one hand side objects allow to handle the string conversion (via the __toString() method), on the other hand side I wonder if such an object should have a string representation or if it better should throw an exception.
Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1790
  • View blog
  • Posts: 5,705
  • Joined: 15-January 14

Re: Array to String Conversion

Posted 10 April 2015 - 10:25 AM

That's probably a question to answer for yourself based on the application and what the object is. If it's not much more than an array wrapper then it might make sense to output the array as something like a JSON or CSV string.
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 4129
  • View blog
  • Posts: 13,023
  • Joined: 08-June 10

Re: Array to String Conversion

Posted 10 April 2015 - 10:29 AM

Quote

based on the application and what the object is.

no application in particular & an extension of the native ArrayObject.

Quote

If it's not much more than an array wrapper then it might make sense to output the array as something like a JSON or CSV string.

is that what you would expect when you printed an array object?
Was This Post Helpful? 0
  • +
  • -

#8 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1790
  • View blog
  • Posts: 5,705
  • Joined: 15-January 14

Re: Array to String Conversion

Posted 10 April 2015 - 11:41 AM

Yeah, I would expect some sort of basic representation of the data, similar to what you would see in the browser's developer console if you used console.log to print an array.
Was This Post Helpful? 0
  • +
  • -

#9 Ornstein  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 13-May 15

Re: Array to String Conversion

Posted 13 May 2015 - 03:04 AM

Remember that there's a difference between printing and converting to a string.

Personally, I believe that "magic" should be used with great care and is one of the reasons why PHP code runs into so much trouble. In this case, I think that supporting conversion of an array object to a string by default is a bad move. Take an example scenario where you've unintentionally passed an array (or array object) where a string was expected; where a PHP warning/error would normally inform you of your mistake, now it risks slipping through the cracks.

To me, it makes more sense to offer methods such as print, toString, toJson, etc. Their intention is clearer semantically and you don't run the same risk of magic letting bugs go unnoticed.
Was This Post Helpful? 1
  • +
  • -

#10 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 4129
  • View blog
  • Posts: 13,023
  • Joined: 08-June 10

Re: Array to String Conversion

Posted 13 May 2015 - 03:29 AM

Quote

Remember that there's a difference between printing and converting to a string.

for clarification, "converting to string" means (string) $object or anything that does this conversion implicitly.

Quote

Take an example scenario where you've unintentionally passed an array (or array object) where a string was expected; where a PHP warning/error would normally inform you of your mistake, now it risks slipping through the cracks.

so, would you prefer a catchable fatal error or an Exception?

Quote

it makes more sense to offer methods such as print, toString, toJson, etc.

  • toString - thatís the method in discussion. although an explicit method for string conversion that does not imply a format (such as "toJson") or purpose (one could think of a "dump" method here) seems pointless to me.
  • print - that does not make any sense in object context as you should never print from inside an object

nevertheless, I agree that there should be methods like "toJson" or "join" that convert the array object into a particular string representation.
Was This Post Helpful? 0
  • +
  • -

#11 Ornstein  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 13-May 15

Re: Array to String Conversion

Posted 13 May 2015 - 03:54 AM

I just threw "print" in there for the sake of example. I don't think it would even be allowed due to it being a reserved word.

When I say toString, I mean an actual toString method, not the __toString magic method. So, if you wanted a string representation of the array, you'd call $whatever->toString().

As for whether I'd prefer an exception or a fatal error, I don't think there's much of a consequential difference between the two. I'd perhaps lean more towards a fatal error because I can't think of a scenario where you would have good reason to continue execution once something like that has happened - and again, an exception runs the risk of unwittingly being caught in the programmer's try/catch.
Was This Post Helpful? 0
  • +
  • -

#12 Ornstein  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 13-May 15

Re: Array to String Conversion

Posted 13 May 2015 - 04:01 AM

I should add: The toString format, if it existed, may just be an agnostic and/or human-readable representation of the data like you would see in Python (['one', 'two', 'etc']) for the sake of testing/debugging. You could instead leave this out and just implement __debugInfo. Again, it's just there for sake of example.
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 4129
  • View blog
  • Posts: 13,023
  • Joined: 08-June 10

Re: Array to String Conversion

Posted 13 May 2015 - 04:45 AM

Iíve checked out __debugInfo(), itís not of use in this case (ArrayObject only has a single property).
Was This Post Helpful? 0
  • +
  • -

#14 Ornstein  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 13-May 15

Re: Array to String Conversion

Posted 13 May 2015 - 04:52 AM

The idea behind __debugInfo would be to output the array's values when the ArrayObject is passed to var_dump.
Was This Post Helpful? 0
  • +
  • -

#15 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 4129
  • View blog
  • Posts: 13,023
  • Joined: 08-June 10

Re: Array to String Conversion

Posted 13 May 2015 - 05:19 AM

unfortunately __debugInfo() does not do that. as per Manual, it defines the objectís properties (and the values to be shown) to be listed in the output.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2