Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67136 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 97821 invoked from network); 24 Apr 2013 19:19:56 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Apr 2013 19:19:56 -0000 Authentication-Results: pb1.pair.com header.from=rasmus@mindplay.dk; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=rasmus@mindplay.dk; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mindplay.dk from 209.85.212.54 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 209.85.212.54 mail-vb0-f54.google.com Received: from [209.85.212.54] ([209.85.212.54:35729] helo=mail-vb0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 47/77-60231-75038715 for ; Wed, 24 Apr 2013 15:19:52 -0400 Received: by mail-vb0-f54.google.com with SMTP id w16so2008861vbf.41 for ; Wed, 24 Apr 2013 12:19:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type:x-gm-message-state; bh=ThqUetyAoSm2H7PBpXoAz9Oc6rxfptVr8rcVcQu/4pc=; b=SUzGDq7fnE+uSnk6LCQwtUgxLNLPPnI2KAbdQyt1j6VdUsfSCIoPoGZVWrRk2LZX7G 7wAPrJlnhmsMwyUWrGyFHCj3KVQhEwNxJGGowcXKtDOqOXSg3M3rKKLu/secp9wmcrpC 193PyCwd8xfbi6OQSdAkUJDuH1+9NbpcE/ND5weh+XYRZei0r01XetBnGe6kRsF5j0kk pjjJbbAPeWFY97G6yrYoXiQymTN5JFimC8GKVi8gs+OY/9eVkVE5S4hrmGI5+xe02UIC 1bkj9/TK9LN/du897CxwKI1iQQO82D4fEegQfT6MNuVnJvDxaK50/9NiRMFw7WqgzamG fWrg== MIME-Version: 1.0 X-Received: by 10.221.3.2 with SMTP id nw2mr22390241vcb.55.1366831189523; Wed, 24 Apr 2013 12:19:49 -0700 (PDT) Received: by 10.58.28.134 with HTTP; Wed, 24 Apr 2013 12:19:49 -0700 (PDT) In-Reply-To: References: Date: Wed, 24 Apr 2013 15:19:49 -0400 Message-ID: To: Anthony Ferrara Cc: Richard Quadling , Richard Bradley , PHP internals Content-Type: multipart/alternative; boundary=089e013a16ec85497904db202fbd X-Gm-Message-State: ALoCoQnLaOG1UuZt/mJc1zTJcFpm3dAJg7wx9aSUDGzO0BMcu0eMPSh82bGX6qbqs4MfKonjBLfH Subject: Re: [PHP-DEV] vsprintf() From: rasmus@mindplay.dk (Rasmus Schultz) --089e013a16ec85497904db202fbd Content-Type: text/plain; charset=ISO-8859-1 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 wrote: > 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 > > --089e013a16ec85497904db202fbd--