Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:46676 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 99428 invoked from network); 13 Jan 2010 10:12:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Jan 2010 10:12:11 -0000 Authentication-Results: pb1.pair.com header.from=ekneuss@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=ekneuss@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 72.14.220.159 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: ekneuss@gmail.com X-Host-Fingerprint: 72.14.220.159 fg-out-1718.google.com Received: from [72.14.220.159] ([72.14.220.159:21627] helo=fg-out-1718.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A7/25-00773-97C9D4B4 for ; Wed, 13 Jan 2010 05:12:10 -0500 Received: by fg-out-1718.google.com with SMTP id l26so44045fgb.11 for ; Wed, 13 Jan 2010 02:12:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to:cc :content-type:content-transfer-encoding; bh=fjji6JsBmTkwLvUGw5020ni1ErqDDv3062LTqOhhkZY=; b=ZDMSGcu1fHq5E3gE699q7kn6VMgIvjCsndRnyTWIX3hwFoyH2ttnv/hmxnN6LO0gjw ghPYLA07+eOIOrHh6kMa9j696KfuMH2CS12+7XP2A5Dk4yyCQJFAEjTw4rfvsaRIwqjs Z0gb+Kz1TMnXdvCM9nGVRw58Y9kmZib9g7wqE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=pvoTI9RcpcBHS+lDvr4fi35yk1yyMEJuJAcDaIu3vvfExmL8beGeW3jK+rZJmHqp6L prdvFpL02c8zd0I7ul5d6b29BxJXIJhwbXyZrL98ENN7oV3ndzphbNRogpOACMoTOQca eeZGp4WEqobkU/CGrthM0dNXFZ2GiNBrole0s= MIME-Version: 1.0 Sender: ekneuss@gmail.com Received: by 10.87.67.20 with SMTP id u20mr6433441fgk.45.1263377525946; Wed, 13 Jan 2010 02:12:05 -0800 (PST) In-Reply-To: References: <3A.94.26332.3E9EB4B4@pb1.pair.com> Date: Wed, 13 Jan 2010 11:12:05 +0100 X-Google-Sender-Auth: 4899ce555ef464e2 Message-ID: To: Chris Stockton Cc: mm w <0xcafefeed@gmail.com>, internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] __toString(), __toArray() From: webmaster@colder.ch (Etienne Kneuss) 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 { >> =A0 =A0 function __catch($data, $type) { >> =A0 =A0 =A0 =A0 =A0 =A0//$type [ static_method, method, get_property, se= t_property] >> =A0 =A0 =A0 =A0 =A0 =A0if (observed && =A0$type =3D=3D set_property && s= omevalueIsObserved) { >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$observer->notify("somevalue::valueWi= llChanged"); >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$this->somevalue =3D $data['somevalue= ']; >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$observer->notify("somevalue::valueDi= dChanged"); >> =A0 =A0 =A0 =A0 =A0 =A0} else { >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0continue__call(); >> =A0 =A0 =A0 =A0 =A0 =A0} >> =A0 =A0 } >> } > > What? ... > >>> Etienne Kneuss wrote: >>> This is where operator over-loading would be useful however perhaps onl= y >>> 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' =3D=3D $type ? 1= 5 : 0; } } > $r =3D 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 compatibl= e 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(). > > 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 > > --=20 Etienne Kneuss http://www.colder.ch