Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58559 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 40372 invoked from network); 4 Mar 2012 02:38:06 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Mar 2012 02:38:06 -0000 Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.49 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 209.85.216.49 mail-qw0-f49.google.com Received: from [209.85.216.49] ([209.85.216.49:53332] helo=mail-qw0-f49.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 63/34-12048-C85D25F4 for ; Sat, 03 Mar 2012 21:38:05 -0500 Received: by qafi29 with SMTP id i29so327342qaf.8 for ; Sat, 03 Mar 2012 18:38:01 -0800 (PST) Received-SPF: pass (google.com: domain of ircmaxell@gmail.com designates 10.224.207.70 as permitted sender) client-ip=10.224.207.70; Authentication-Results: mr.google.com; spf=pass (google.com: domain of ircmaxell@gmail.com designates 10.224.207.70 as permitted sender) smtp.mail=ircmaxell@gmail.com; dkim=pass header.i=ircmaxell@gmail.com Received: from mr.google.com ([10.224.207.70]) by 10.224.207.70 with SMTP id fx6mr2305804qab.79.1330828681813 (num_hops = 1); Sat, 03 Mar 2012 18:38:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=cYx/YjzC1VVOjtvRE5o8AE4QC4SjOGvGdq6FsOZA/M0=; b=OkV+9w5aTdUK4/X5ukG+DFPIcW2bv4HqQOLdZDj+kA0yQnn5YSll5CxHrOD1GG6Vrk GuOljOPTUCK32EfNd2lj2D9/eA1q+NCo3iiSbK11+pmBzmIcIxiDNX1x/zd26Nt6Hljj SAltyUIGh1dZBqj5RjckVQ6bKbhi5k/uJPdp885oQmlw/bu4iypuUtqZXzTvnFe5XqCU YgORfHhmAt/Xsb1ZF+6HTXoyCFDlHQ0a2pnp05yIwxlALG8ZtvBFx0bPbNc2VBltaw0p 3yrl0W8FJY6CJdRqCeK91aCEK1M1KsJC8hJwawnLWD+u+ZKYRRmG8xN0gKPSRY2auNIL WxVw== MIME-Version: 1.0 Received: by 10.224.207.70 with SMTP id fx6mr1986243qab.79.1330828681614; Sat, 03 Mar 2012 18:38:01 -0800 (PST) Received: by 10.229.49.74 with HTTP; Sat, 3 Mar 2012 18:38:01 -0800 (PST) In-Reply-To: <4F52D301.5060101@sugarcrm.com> References: <4F52D301.5060101@sugarcrm.com> Date: Sat, 3 Mar 2012 21:38:01 -0500 Message-ID: To: Stas Malyshev Cc: "internals@lists.php.net" Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] [RFC - discussion] Object Scalar Type Casting Magic Methods From: ircmaxell@gmail.com (Anthony Ferrara) Stas, On Sat, Mar 3, 2012 at 9:27 PM, Stas Malyshev wrote: >> https://wiki.php.net/rfc/object_cast_to_types > > Some notes: > > 1. Use cases for this are unclear. Wrapping scalar types does not seem very > efficient as conversion is always one-directional - once you do any > operation your type is gone, so unless you re-wrap it each time (at which > point you could as well un-wrap it manually) it's not very useful. > Enabling passing SplFixedArray to array functions is very simple - as with > any iterable type - just use iterator_to_array (if you don't have builtin > convertor, which SplFixedArray does). But it's not always a good idea - > since it creates a full copy. Both this method and your conversion do not > have a context so it is not possible to avoid creating a full copy. That's quite fair and understandable. And something I'm hoping people on the list here will be able to help out with. I know this is an often requested feature (and I've hit cases where "it would be nice if"). I just can't think of any good ones now. One thing to note is that this becomes much more useful if implemented along-side the other RFC I proposed with parameter casting ( https://wiki.php.net/rfc/parameter_type_casting_hints ). Another thing to consider (or, let me propose), is that consistency is a reason to add this. Right now, we can cast to string types and control the cast, but we can't to any other type. So this would open the ability for consistent casting across all scalar (non-object) types... > 2. Having separate methods is probably not the best idea. It may lead to > implementation having inconsistent conversions between types, such as floats > and integers, etc. or having conversions to float but not int and getting > very hard to debug errors because of that. Also may lead to conversion be > context-dependent and create very confusing results. Well, as you know that was part of my original proposal ( https://wiki.php.net/rfc/object_cast_magic ). However, after talking with a few core devs about it, it seemed that separate methods are the preferred solution, as the single method requires a big switch (which isn't as clean, and will promote returning improper types, Floats when an int was asked for, etc)... For that reason, it seems more fragile to maintain, so I agreed with splitting it to multiple methods. But either way, if people feel strongly, I would be willing to change the implementation... > 3. There's already cast_object handler, why not use it? I'm not sure messing > with get is a good idea, it's not exactly meant for that. cast_object goes to zend_cast_object_tostring(), which is specifically hard-coded to return specific values for types. So rather than edit that, I implemented a wrapper on-top to implement these additions. That way, the current (BC) functionality remains untouched... But I could see pushing this code into that handler. My only concern there is naming (it's called tostring, but it does far more than that)... As far as the get() handler, I can see the uses, but if it's really that big of an issue (not saying you're making it out to be), I think we can remove that part of the proposal without substantially affecting the concept of the patch and RFC... Thanks for the comments Anthony