Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60576 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 66093 invoked from network); 14 May 2012 11:45:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 May 2012 11:45:01 -0000 Authentication-Results: pb1.pair.com header.from=seva.lapsha@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=seva.lapsha@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.42 as permitted sender) X-PHP-List-Original-Sender: seva.lapsha@gmail.com X-Host-Fingerprint: 209.85.213.42 mail-yw0-f42.google.com Received: from [209.85.213.42] ([209.85.213.42:47597] helo=mail-yw0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AA/F3-44671-B30F0BF4 for ; Mon, 14 May 2012 07:45:00 -0400 Received: by yhfq11 with SMTP id q11so4866842yhf.29 for ; Mon, 14 May 2012 04:44:57 -0700 (PDT) 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=KoYaFSaXYweRPrScGOsCbR97myoxVGpA0r5sOUcGwv4=; b=dzCBbAygESeLbUdmMy74yyQhiIK7tJWQQGFqsioEiCuCF9sFQD3Ks0hPcZSgnhfl/D RZlI6hJd7Dt1k2Bv+zUROeqYVWrDiv3bcTfcCu2nvBWktDteTTss7aAWtvGqD3vvTQjx npFnqqo37p3P4pE9+aerpsSg5EDr8Mki+WNMAbxk6qnnuJ1TAgOrSmpazX3jqtSzz5g8 /y3UX5eV/LRpH28iST2MY5j4GHoXnf7DBeGoSajw60aW+hPloH+3CgpMwW1cAJkJzzAA 4g9O7/toItBmj7CY7ya/xhxTDKr9Db0StjX+wlXpzdtUE1QUV9KaUmwgVoJjT/nXxgHF 1pyA== MIME-Version: 1.0 Received: by 10.50.180.138 with SMTP id do10mr2469769igc.36.1336995897020; Mon, 14 May 2012 04:44:57 -0700 (PDT) Received: by 10.231.241.199 with HTTP; Mon, 14 May 2012 04:44:56 -0700 (PDT) In-Reply-To: <4FB04675.6000307@sugarcrm.com> References: <4FB04675.6000307@sugarcrm.com> Date: Mon, 14 May 2012 07:44:56 -0400 Message-ID: To: Stas Malyshev Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=14dae934060b8210cb04bffd9d6e Subject: Re: [PHP-DEV] [RFC] Custom Casting From: seva.lapsha@gmail.com (Seva Lapsha) --14dae934060b8210cb04bffd9d6e Content-Type: text/plain; charset=ISO-8859-1 Hi, Not quite. The proposed is a syntactic sugar which is thought to handle any transformation of a value, not necessarily or limited to type or class conversion. It is of course possible to limit the usage to just that, with any user defined convention or "best practice". In fact it's pretty distinct from primitive casting, I just had in mind that reusing the casting syntax could be an advantage due to similarity of the behavior. In simple words the statements of *$var = (ClassName)$var* or *function(ClassName $var){}* would not be read as "Cast to", but "Cast with". The example suggestion with wrapping the value in an object just for handling value validation/sanitization is not just overkill, but also is excess, since there is no any need to have the value wrapped after the function input processing. In fact, the closest construct to the mentioned is: function foo(/* to be casted with PositiveInteger */ $i) { $i = PositiveInteger::cast($i); return 2*sqrt($i); } Hope this makes sense. On Sun, May 13, 2012 at 7:40 PM, Stas Malyshev wrote: > Hi! > > > Please be kind to review and comment my proposal for custom casting in > PHP > > (or let me know if a similar proposal was already discussed). > > > > http://pastebin.com/sPb0i8U6 > > I think there's an issue with this idea. As far as I understand, what is > proposed is kind of cast-constructor paradigm. However, the result of > (ClassName)$var would be expected to be an object of type ClassName. > That is, however, not what cast() returns - it returns integer $var. > Since there's no way for foo(ClassName $var) to know where $var came > from, the result of $var = (ClassName)$var; is just a simple integer and > foo() would reject it since it's not an object of type ClassName. > > Of course, you could just create this instead: > > class PositiveInteger { > private $i; > function __construct($i) { > if(intval($i) < 1) $i = 1; > $this->i = $i; > } > function intval() { > return $this->i; > } > } > > function foo(PositiveInteger $i) { return 2*sqrt($i->intval()); } > > and then: > > foo(new PositiveInteger(42)); > > But this is doable right now, even if sounds a bit of overkill for me. I > wouldn't mind also having (int)$i done with some magic methods, as > proposed in RFCs you mentioned. > > -- > Stanislav Malyshev, Software Architect > SugarCRM: http://www.sugarcrm.com/ > (408)454-6900 ext. 227 > --14dae934060b8210cb04bffd9d6e--