This is all kinds of wrong:
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
Do I need to explain in detail why this is all kinds of effed up?
Yes, I'd like that explanation.
This is all kinds of wrong:
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
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:
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
This is all kinds of wrong:
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
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
-----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:
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
On 24 April 2013 17:31, Richard Bradley Richard.Bradley@softwire.comwrote:
-----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:
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
Oh! Another magic method opportunity ...
/**
- Operates just like __toString(), but returns an array.
*/
public function __toArray();
(ducking)
--
Richard Quadling
Twitter : @RQuadling
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
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.comwrote:
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
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
I'm only half joking.
--
Richard Quadling
Twitter : @RQuadling
EE : http://e-e.com/M_248814.html
Zend : http://bit.ly/9O8vFY