Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67927 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 3894 invoked from network); 27 Jun 2013 09:04:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Jun 2013 09:04:35 -0000 Authentication-Results: pb1.pair.com header.from=tom@sclinternet.co.uk; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=tom@sclinternet.co.uk; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain sclinternet.co.uk from 209.85.212.53 cause and error) X-PHP-List-Original-Sender: tom@sclinternet.co.uk X-Host-Fingerprint: 209.85.212.53 mail-vb0-f53.google.com Received: from [209.85.212.53] ([209.85.212.53:56220] helo=mail-vb0-f53.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 74/1A-51393-0200CC15 for ; Thu, 27 Jun 2013 05:04:33 -0400 Received: by mail-vb0-f53.google.com with SMTP id p12so392194vbe.26 for ; Thu, 27 Jun 2013 02:04:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :x-gm-message-state; bh=Ze3XGid80wALQII5BwKDWa89ohWgrG4YRouOXG49Tdg=; b=BDL2wL25zkmPCWK8bcMr0UmfjsyY1UnrzBPCoc/NdwarYTF/QVwx/me1Wt6L+yDuNX hNa7oKbRAQACiC/qYTwfgJWRpMRtGRduUKlYJ1aO0tT1e4Ss3ns1Da0x3hi0hoHiCel5 pr7GeFPGUHSSM73hyitjI+VNQ2/WfEL1jnRIXWF3/FagoAx+Cs7CNukhVNd+1PotGMZN DQG7KG9bgI9bjcyXT6WyQV5f83Em3ahNh4BwbZsFOIXqPNYc8TnXNQFKW8mPzrAn64ya p1RLyvYtsqYgLsFqL7Zaf2wglbTu5SCVWVbBUIbHqz6Jtm61pWPIV+P0ImB1bc/cVfpA oK7Q== MIME-Version: 1.0 X-Received: by 10.58.100.234 with SMTP id fb10mr3321759veb.5.1372323869609; Thu, 27 Jun 2013 02:04:29 -0700 (PDT) Sender: tom@sclinternet.co.uk Received: by 10.52.103.108 with HTTP; Thu, 27 Jun 2013 02:04:29 -0700 (PDT) In-Reply-To: References: Date: Thu, 27 Jun 2013 10:04:29 +0100 X-Google-Sender-Auth: 9tX38et9-AWbKEkHpLIj-GX-bfQ Message-ID: To: RQuadling@gmail.com Cc: PHP internals Content-Type: multipart/alternative; boundary=089e013a2746c3e2a704e01f0c7d X-Gm-Message-State: ALoCoQndwBRCgO51uOWmuJsoXX4gxPiD3mHGb3SCSkJpmCR6atEaqhErJbbbsDTseldspagujmiq Subject: Re: [PHP-DEV] RFC Proposal: New assign value operator From: tom@scl.co.uk (Tom Oram) --089e013a2746c3e2a704e01f0c7d Content-Type: text/plain; charset=ISO-8859-1 Hi Richard, Thanks for your reply, the main reason would be operator overloading rather than the typecasting example, the typecasting version is more for consistency. I am also fairly that there might be situations where it would be useful to set the value of a scalar while preserving the time and save the need for checking the type first, however I do admit I can't think of a concrete example. The main thing I was thinking is I often find myself writing things like: $obj->set(2); $obj->setValue(5); $obj->setX(4); Where the object only really represent a single value, examples are things like EmailAddress class, ZipCode class, MoneyValue class, etc. Everytime I write something like one of the examples above I wish I could simply write: $email = 'tom@scl.co.uk'; and continue to use my EmailAddress class, however since PHP is loosely type this replaces the object with a string, hence my suggesting for new operator. All the best, Tom On 26 June 2013 11:51, Richard Quadling wrote: > > > > On 25 June 2013 11:01, Tom Oram wrote: > >> Hi everyone, >> >> I've got an idea for an RFC proposal and from reading the instructions it >> looks like I should run it past you guys first. >> >> I have not made any contributions to PHP before although I have made some >> custom modifications in house in the past and while I'm no longer familiar >> with the PHP code base I am confident I have the skills to implement this >> should it be accepted. >> >> What I want to propose is a new assignment operator which rather than >> setting the variable to completely new value it would update the value >> while maintaining the type. >> >> The main motivation for this is for easy assignment to value objects aka >> assignment operator overloading via a magic method (prehaps called >> __assign). >> >> Here is an example ( for now I will use the PASCAL style assignment >> operator := as the new operator as it is a know assignment operator and >> currently not used in PHP): >> >> // For a class defined like so... >> class MoneyValue >> { >> protected $amount; >> >> public function __assign($value) >> { >> $this->amount = $value; >> } >> } >> >> // The amount could then be assigned using the new operator like this >> >> $price = new MoneyValue(); >> >> $price := 29.99; >> >> While the primary focus would be for assignment operator overloading as I >> just displayed in the previous example, for consistency it could be used >> with scalar values to preserve type like so: >> >> // $str is now a string >> >> $str = 'Original String'; >> >> // Using the new assignment variable would cast the value being assigned >> to >> the variable's type >> // (in this case a string). So >> >> $str := 7; >> >> // Would be the equivalent to >> // >> // $str = (string) 7; >> // >> // $str === "7" >> >> >> Another quick example: >> >> $num = 5; >> >> $num := '12'; >> >> // Equivalent to >> // >> // $num = (int) '12'; >> // >> // $num === 12; >> >> So what do you guys think? >> >> If I get a good response I'll look into how to create a proper RFC and >> start trying to work out how to implement it. >> >> Many thanks and look forward to some responses, >> Tom >> > > Hi. > > I'm not going to comment on the merits as such, but I'd be interested in > knowing what problem this RFC would solve? > > Considering PHP has type juggling scalars, it would SEEM (I may have > missed the point) that this could result in data loss when the enforced > conversion results in 0/False/"". > > $num = 8; > $num := "data from user"; // 0 > > Unless that is the expected behaviour. > > Where would this RFC change be used? > > And having said all of that, I'm not against it, just want to see what it > would be useful for that isn't already part of PHP's toolbox. > > -- > Richard Quadling > Twitter : @RQuadling > EE : http://e-e.com/M_248814.html > Zend : http://bit.ly/9O8vFY > -- **************************************************** PLEASE NOTE: For support requests please use support@scl.co.uk instead of emailing staff directly, this way your request is likely to be dealt with more efficiently. **************************************************** Tom Oram - SCL Internet Services PO Box 8, Cardigan, Ceredigion, SA41 3YA Website: http://www.scl.co.uk/ Tel: +44 (0) 1239 622 411 Fax: +44 (0) 1239 622428 Company Reg. No. 2441708 **************************************************** --089e013a2746c3e2a704e01f0c7d--