# vsprintf()

- Thread: https://externals.io/message/67125
- Original archive: https://news-web.php.net/php.internals/67125
- Started by Rasmus Schultz on 2013-04-24T15:45:12+00:00
- Messages: 10
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #67125 — Rasmus Schultz — 2013-04-24T15:45:12+00:00

URL: https://externals.io/message/67125#67125

This is all kinds of wrong:

http://3v4l.org/UZFME

So the order in which the properties were defined is the magic that makes
this work.

Wow. WTF?

Do I need to explain in detail why this is all kinds of effed up?

- Rasmus Schultz

---

## #67126 — Levi Morrison — 2013-04-24T15:49:23+00:00

URL: https://externals.io/message/67125#67126 · In reply to: #67125

>
> Do I need to explain in detail why this is all kinds of effed up?
>
>
Yes, I'd like that explanation.

---

## #67127 — johannes@schlueters.de — 2013-04-24T15:53:58+00:00

URL: https://externals.io/message/67125#67127 · In reply to: #67125

> This is all kinds of wrong:
> 
> http://3v4l.org/UZFME
> 
> So the order in which the properties were defined is the magic that makes
> this work.
> 
> Wow. WTF?
> 
> Do I need to explain in detail why this is all kinds of effed up?

 `vsprintf()`'s 2nd parameter is an array,so your objectis converted to an
array ['str1' => 'foo', 'str2'=>'bar'] and PHP arrays are ordered.

johannes

---

## #67128 — Richard Bradley — 2013-04-24T16:31:15+00:00

URL: https://externals.io/message/67125#67128 · In reply to: #67125

> -----Original Message-----
> From: Rasmus Schultz [mailto:rasmus@mindplay.dk]
> Sent: 24 April 2013 16:45
> To: PHP internals
> Subject: [PHP-DEV] `vsprintf()`
>
> This is all kinds of wrong:
>
> http://3v4l.org/UZFME
>
> So the order in which the properties were defined is the magic that makes this work.
>
> Wow. WTF?
>
> Do I need to explain in detail why this is all kinds of effed up?

Yes, please.

What's the big deal here? If you coerce an object to an array, you get the public
properties in the order in which they were declared.

See http://www.php.net/manual/en/language.types.array.php

 "If an object is converted to an array, the result is an array whose elements are the
  object's properties. The keys are the member variable names, with a few notable
  exceptions: ...."

What was the point of your email? Did you hope for some action to arise, or are
you just letting off steam?

Best,


Rich



Richard Bradley
Tel : 020 7485 7500 ext 3230 | Fax : 020 7485 7575

softwire
Sunday Times Best Small Companies 2012 - 6th in the UK
Web : www.softwire.com | Addr : 325 Highgate Studios, 53-79 Highgate Road, London NW5 1TL
Softwire Technology Limited. Registered in England no. 3824658. Registered Office : 13 Station Road, London N3 2SB

---

## #67129 — Richard Quadling — 2013-04-24T16:36:36+00:00

URL: https://externals.io/message/67125#67129 · In reply to: #67128

On 24 April 2013 17:31, Richard Bradley <Richard.Bradley@softwire.com>wrote:

> > […]
> makes this work.
> > […]
>
> Yes, please.
>
> What's the big deal here? If you coerce an object to an array, you get the
> public
> properties in the order in which they were declared.
>
> See http://www.php.net/manual/en/language.types.array.php
>
>  "If an object is converted to an array, the result is an array whose
> elements are the
>   object's properties. The keys are the member variable names, with a few
> notable
>   exceptions: ...."
>
> What was the point of your email? Did you hope for some action to arise,
> or are
> you just letting off steam?
>
> Best,
>
>
> Rich


Oh! Another magic method opportunity ...

/**
 * Operates just like __toString(), but returns an array.
 */
public function __toArray();


(ducking)

-- 
Richard Quadling
Twitter : @RQuadling

---

## #67130 — Anthony Ferrara — 2013-04-24T16:39:03+00:00

URL: https://externals.io/message/67125#67130 · In reply to: #67129

Richard,


Oh! Another magic method opportunity ...
>
> /**
>  * Operates just like __toString(), but returns an array.
>  */
> public function __toArray();
>
>
> (ducking)


I know you're joking, but this has been brought up before (and I intend to
bring it up again): https://wiki.php.net/rfc/object_cast_to_types

---

## #67135 — Ralph Schindler — 2013-04-24T17:54:14+00:00

URL: https://externals.io/message/67125#67135 · In reply to: #67127

Well, since I was the one who posted it 
(https://twitter.com/ralphschindler/status/327084619507855361), I'll 
further explain it.  I realized it was doing this when actually running 
this kind of snippet:

http://3v4l.org/ZkE6B

I must have left the public in there when I posted it to twitter.  I'd 
have to say it's a little unintuitive when the properties are protected, 
but all in all, I don't dislike the behavior.  Perhaps this just needs 
to be documented?

-ralph


> 
> > […]
>
> `vsprintf()`'s 2nd parameter is an array,so your objectis converted to an
> array ['str1' => 'foo', 'str2'=>'bar'] and PHP arrays are ordered.
>
> johannes

---

## #67136 — Rasmus Schultz — 2013-04-24T19:19:49+00:00

URL: https://externals.io/message/67125#67136 · In reply to: #67130

Frankly, a magic method sounds like a much better solution than
auto-magically converting objects to arrays.

The problem with automatic conversion, is that the order of properties is
an implementation detail - the `vsprintf()` example perfectly illustrates the
problem:

class User
{
    public $first = 'Joe';
    public $last = 'Shmoe';
}

$user = new User;

echo vsprintf('Welcome, %s %s !', $user);


Refactor as follows:

class User
{
    public $age = 37;
    public $first = 'Joe';
    public $last = 'Shmoe';
}


"Welcome, 37 Joe !"

The fact that somebody even came up with the `vsprintf()` example is scary.

I'm not going to get into how this gets worse when things like inheritance
further makes the order unpredictable.

There is likely only one valid use-case for this, which is converting an
object to an array for the purpose of serialization or other rare cases of
iterating over properties - but we can already handle that with an array()
typecast, yes?

At least the explicitness of the typecast provides some indication of what
you're doing or what to expect.

Magically converting for that one use-case is like an invitation to create
ugly, obscure, unreadable code. (and make mistakes.)

Name a good, valid use-case for this feature ?

On Wed, Apr 24, 2013 at 12:39 PM, Anthony Ferrara <ircmaxell@gmail.com>wrote:

> Richard,
>
>
> Oh! Another magic method opportunity ...
> > […]
>
>
> I know you're joking, but this has been brought up before (and I intend to
> bring it up again): https://wiki.php.net/rfc/object_cast_to_types

---

## #67139 — johannes@schlueters.de — 2013-04-25T07:24:14+00:00

URL: https://externals.io/message/67125#67139 · In reply to: #67135

> 
> 
> I must have left the public in there when I posted it to twitter.
> I'd 
> have to say it's a little unintuitive when the properties are
> protected, 
> but all in all, I don't dislike the behavior.  Perhaps this just
> needs 
> to be documented? 

If anything in
http://php.net/language.types.array#language.types.array.casting
is not clear enough please report a documentation bug (or fix it)

johannes

---

## #67142 — Richard Quadling — 2013-04-25T09:07:31+00:00

URL: https://externals.io/message/67125#67142 · In reply to: #67130

> Richard,
>
>
> Oh! Another magic method opportunity ...
> > […]
>
>
> I know you're joking, but this has been brought up before (and I intend to
> bring it up again): https://wiki.php.net/rfc/object_cast_to_types
>
>

I'm only half joking.

-- 
Richard Quadling
Twitter : @RQuadling
EE : http://e-e.com/M_248814.html
Zend : http://bit.ly/9O8vFY
