Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:46684 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 33498 invoked from network); 13 Jan 2010 13:01:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Jan 2010 13:01:23 -0000 X-Host-Fingerprint: 75.72.251.154 c-75-72-251-154.hsd1.mn.comcast.net Received: from [75.72.251.154] ([75.72.251.154:10309] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F9/DB-00773-224CD4B4 for ; Wed, 13 Jan 2010 08:01:23 -0500 Message-ID: To: internals@lists.php.net Date: Wed, 13 Jan 2010 07:01:19 -0600 User-Agent: Thunderbird 2.0.0.23 (Windows/20090812) MIME-Version: 1.0 References: <3A.94.26332.3E9EB4B4@pb1.pair.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 75.72.251.154 Subject: Re: [PHP-DEV] __toString(), __toArray() From: cpriest@warpmail.net (Clint Priest) Etienne Kneuss wrote: > Hello, > > On Tue, Jan 12, 2010 at 11:40 PM, Chris Stockton > wrote: >> Hello, >> >> On Mon, Jan 11, 2010 at 8:32 PM, mm w <0xcafefeed@gmail.com> wrote: >>> cast is not needed in PHP >>> >>> i 'd rather be more interesting in >>> >>> class Obj { >>> function __catch($data, $type) { >>> //$type [ static_method, method, get_property, set_property] >>> if (observed && $type == set_property && somevalueIsObserved) { >>> $observer->notify("somevalue::valueWillChanged"); >>> $this->somevalue = $data['somevalue']; >>> $observer->notify("somevalue::valueDidChanged"); >>> } else { >>> continue__call(); >>> } >>> } >>> } >> What? ... >> >>>> Etienne Kneuss wrote: >>>> This is where operator over-loading would be useful however perhaps only >>>> explicit casts would make sense here. >> I beleive adding a __cast(string $type) would be a useful feature for >> me, very often I have a toArray method defined. I agree with you that >> due to unexpected edge cases with operator precedence and general type >> juggling that __cast should only be called on explicit (cast). >> >> I.E.: >> class Int { public function __cast($type) { return 'int' == $type ? 15 : 0; } } >> $r = new Int; > > This is an exemple where __cast would be very wrong. At the very > least, it should return a value that is an inhabitant of the requested > type! Do you really want (bool)new Int() to be Int 0? > > as a result, you have three solutions: > 1) have the engine issue an error in case the return value is not compatible > 2) provide a return value for every possible types requested > 3) throw an exception in case the cast is not handled > > In the end, it looks way more complicated/magic than _simply_ defining > a toArray method that is used whenever you need an object to be an > array. You need an explicit cast anyway, so it's (array)$obj vs > $obj->toArray(). One difference between these two possibilities is that consumers of an array parameter could consume an object castable to an array whereas it would need to have knowledge that ->toArray() exists in the second scenario. > >> var_dump($r + 1); // 2 >> var_dump((int) $r + 1); // 16 >> var_dump((bool) $r + 1); // 1 >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> > > >