Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58313 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 26412 invoked from network); 29 Feb 2012 00:37:57 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Feb 2012 00:37:57 -0000 Authentication-Results: pb1.pair.com smtp.mail=kris.craig@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=kris.craig@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.42 as permitted sender) X-PHP-List-Original-Sender: kris.craig@gmail.com X-Host-Fingerprint: 209.85.212.42 mail-vw0-f42.google.com Received: from [209.85.212.42] ([209.85.212.42:49667] helo=mail-vw0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B8/23-36673-2637D4F4 for ; Tue, 28 Feb 2012 19:37:56 -0500 Received: by vbjk13 with SMTP id k13so2481552vbj.29 for ; Tue, 28 Feb 2012 16:37:51 -0800 (PST) Received-SPF: pass (google.com: domain of kris.craig@gmail.com designates 10.52.25.107 as permitted sender) client-ip=10.52.25.107; Authentication-Results: mr.google.com; spf=pass (google.com: domain of kris.craig@gmail.com designates 10.52.25.107 as permitted sender) smtp.mail=kris.craig@gmail.com; dkim=pass header.i=kris.craig@gmail.com Received: from mr.google.com ([10.52.25.107]) by 10.52.25.107 with SMTP id b11mr15671552vdg.37.1330475871702 (num_hops = 1); Tue, 28 Feb 2012 16:37:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=qNa9IqvwMf+QU/XAC5F/4ivFT5nZMsBnQ69/qoYjZXg=; b=UqS/XPZkpQD5xeqhpABXg7JU1u/fq1g2GTRJR4ByHcCXCxebcsQwICO/KR5Gb7QPDG BYtWuIhO8BELckmXwlnlwLYI5oAvmjQkZzV8kZJCxiUWHCdxruNm4elMU+sqUeYZZTUH UzKrrwQ/5er4/GYbM+ZDRDBRb20sJQbAaVHkk= MIME-Version: 1.0 Received: by 10.52.25.107 with SMTP id b11mr12977930vdg.37.1330475871477; Tue, 28 Feb 2012 16:37:51 -0800 (PST) Received: by 10.52.91.5 with HTTP; Tue, 28 Feb 2012 16:37:51 -0800 (PST) In-Reply-To: References: <6d4a9f99bc550c8c673b32cbccc52444.squirrel@www.l-i-e.com> <29360b43e9c23389dac011c6a53332bc.squirrel@www.l-i-e.com> Date: Tue, 28 Feb 2012 16:37:51 -0800 Message-ID: To: Anthony Ferrara Cc: Richard Lynch , internals@lists.php.net Content-Type: multipart/alternative; boundary=20cf307d0416b39fe804ba0f8d74 Subject: Re: [PHP-DEV] Object Casting - An Alternative to Type Hinting From: kris.craig@gmail.com (Kris Craig) --20cf307d0416b39fe804ba0f8d74 Content-Type: text/plain; charset=ISO-8859-1 I think this is +1 for moving the conversation to a less crowded location. Sorry guys I know I keep promising to take care of it but I've been swamped all day. I'll try to find some time though. --Kris On Tue, Feb 28, 2012 at 4:28 PM, Anthony Ferrara wrote: > Richard, > > This is not overloading addition. Not by a long shot. > > This is overloading casting to primitives (which happens in the case > of addition, but a bunch of others). It turns out that the addition > operator calls object->get in C to get the primitive value of the > object if it exists. That's where this works. So: > > > This obviously trivial example makes perfect sense, until you realize > > that some moron (and PHP has lots of them) will sooner or later make > > ++ do something completely weird, and I'm going to have to edit their > > code-base and will have to dig through umpteen class files to figure > > out what the heck ++ means... > > Yes, the example is trivial. And yes, it can be abused. But I think > you're blowing it WAY out of proportion. ++ still means what it > always has meant. It's the *exact* same as normal (addition is still > happening). > > The only change is where the addition gets its value, and where that > value is stored. > > And it's not really digging through umpteen classes to figure out what > the cast is. To be honest, you could say the exact same thing about > polymorphism. And the indirection there is actually seen as a *good* > thing. So I'm not sure what argument you're trying to make aside from > that you don't like it... > > > And then god knows what happens when you do: > > > > $foo = new Foo(); //from library A > > $bar = new Bar(); //from library B > > $foobar = $foo + $bar; > > //Will this last statement even make sense, even though both Foo and > > Bar override the + operator, just in entirely different ways... > > Neither override anything. let me say this again: THIS IS NOT DOING > OPERATOR OVERLOADING. > > That's 100% predictable. Lookup Foo and Bar, and see if either/both > implement __castTo. If so, the types they return will tell you > exactly what it will be. > > > How can you even guarantee that $foo, presumably taking precedence, > > won't attempt to access a non-existent property of Bar, generating an > > ignored E_NOTICE on most hosts, and introduce a subtle bug that nobody > > notices for months, even years, in a seldom-used function? > > You don't need to. Because $foo will never know about $bar (or vise > versa)... > > And to be honest, you could say the same thing about LOTS of existing > NON-MAGIC functionality. So I don't see that this contributes to the > conversation at all... > > > Your proposal lets one re-define + to mean whatever one wants, and > > while you might not abuse that, somebody else will... > > Ok, This is rediculous. Open the patch. Tell me where it redefines > +, or even remotely can be used to... + is still a scalar operator, > and with this patch you cannot change that. The only thing this patch > lets you do with respect to the + operator is choose what the scalar > representation of the object is... > > > > > Personal Note: > I appreciate replies and concerns. But at least read the patch and > try to understand what the POC does before going off on a rant that > does nothing but turn people off unless you actually realize what's > going on. If you don't understand what it does, then ask. But don't > complain about things the feature doesn't even touch, it's > disrespectful and doesn't accomplish anything but to add noise to the > conversation. > > Anthony > > > On Tue, Feb 28, 2012 at 4:37 PM, Richard Lynch wrote: > > On Mon, February 27, 2012 11:05 pm, Anthony Ferrara wrote: > >> >> class Foo { > >> public $value = 1; > >> } > >> class Bar { > >> public $value = 1; > >> public function __castTo($type) { > >> return $this->value; > >> } > >> public function __assign($value) { > >> $this->value = $value; > >> return $value != 10; > >> } > >> } > >> > >> $a = new Foo; > >> $b = new Bar; > >> $a++; > > > > So, here's the thing... > > > > This obviously trivial example makes perfect sense, until you realize > > that some moron (and PHP has lots of them) will sooner or later make > > ++ do something completely weird, and I'm going to have to edit their > > code-base and will have to dig through umpteen class files to figure > > out what the heck ++ means... > > > > I can see a lot of legitimate serious coders using this wisely. > > > > Unfortunately, I can see a million scripters using it very un-wisely. > > > > I can even see people overloading the plus operator for an object > > wrapped around an array in multiple different ways, then pulling in > > different libraries that seem quite useful, and the only way I can > > figure out what "+" means is to dig through even more files and figure > > out which bastard child this object comes from, that was passed into > > some random function that uses + on it. > > > > And then god knows what happens when you do: > > > > $foo = new Foo(); //from library A > > $bar = new Bar(); //from library B > > $foobar = $foo + $bar; > > //Will this last statement even make sense, even though both Foo and > > Bar override the + operator, just in entirely different ways... > > > > How can you even guarantee that $foo, presumably taking precedence, > > won't attempt to access a non-existent property of Bar, generating an > > ignored E_NOTICE on most hosts, and introduce a subtle bug that nobody > > notices for months, even years, in a seldom-used function? > > > > Consider the trivial example where Foo uses array_sum() and Bar uses > > count() comes up with a number. It's a totally meaningless number in > > any real-world scenario. How would one detect this bug, really, in > > some mish-mash of libraries from PEAR/PECL/homebrew-MVC/Framework. > > > > I'm sure you can define the "rules" in a page or so for precedence in > > type-casting and what happens when. > > > > But I really am not interested in figuring out the "rules" for + when > > I've been using it the same way since 2nd grade, with modest changes > > over the years that follow the spirit of "+" > > > > Your proposal lets one re-define + to mean whatever one wants, and > > while you might not abuse that, somebody else will... > > > > You might as well install runkit on everybody's server and let them > > have at it. :-) > > > > PHP was founded on the KISS principle, so that newbies can, in fact, > > write sub-optimal but working code quickly to get a website up, and to > > keep newbie fingers out of mashing gears. > > > > This is way over-the-top from that principle. > > > > -- > > brain cancer update: > > http://richardlynch.blogspot.com/search/label/brain%20tumor > > Donate: > > > https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=FS9NLTNEEKWBE > > > > > > > > -- > > PHP Internals - PHP Runtime Development Mailing List > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --20cf307d0416b39fe804ba0f8d74--