Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:46650 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 4780 invoked from network); 12 Jan 2010 03:17:56 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Jan 2010 03:17:56 -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:23246] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3A/94-26332-3E9EB4B4 for ; Mon, 11 Jan 2010 22:17:56 -0500 Message-ID: <3A.94.26332.3E9EB4B4@pb1.pair.com> To: internals@lists.php.net Date: Mon, 11 Jan 2010 21:17:53 -0600 User-Agent: Thunderbird 2.0.0.23 (Windows/20090812) MIME-Version: 1.0 References: 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 Mon, Jan 11, 2010 at 7:31 PM, Clint Priest wrote: >> I know there's been requests to add a __toArray() and most of the arguments >> against it is that there are too many magic functions already. I just >> thought I'd propose an alternative that would satisfy all of them. >> >> Why not a __cast($Type) magic function? > > I'd advance two reasons against this idea: > > 1) It's more self-explanatory to explicitly call the appropriate > converting method, enough with implicit madness! > > 2) For some operations, you'd have to know the types in advance before > knowing what operations needs to be performed: > > $obj1 + 2; > > Now what __cast should be called? Int? Float? Wouldn't it make sense to be an integer since its being added to an integer? > > Also, what about $obj1 + $obj2: Int? Float? Array? This is where operator over-loading would be useful however perhaps only explicit casts would make sense here. > > Another example: str_replace($obj1, "bar", "foo"); what to call? > __toString or __toArray? str_replace accepts both. This is an interesting situation, could be resolved by attempting a __cast('array') followed by a __cast('string') if the first call was rejected (false, exception or something). > > IMHO it would only make sense to invoke methods on explicit casts > only, otherwise it will just be a mess with PHP's current type > juggling. But, as we seen with __toString, limiting the field of > application was annoying (and it was later extended to nearly all > string usages). I would agree with this as well, only called during explicit casts. > > So, what will it be? Inconsistent and Confusing or Limited and Annoying? > One other alternative, probably not as easy would be to pass multiple types to cast, much the way the Accepts: HTTP header does, providing a list of possible cast types, in the preferred order. One could even implement an interface such as: interface Castable { function GetCastableTypes(); } or some such. >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> > > >