Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:39331 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 37860 invoked from network); 25 Jul 2008 18:47:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 25 Jul 2008 18:47:03 -0000 Authentication-Results: pb1.pair.com smtp.mail=helly@php.net; spf=unknown; sender-id=unknown Authentication-Results: pb1.pair.com header.from=helly@php.net; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain php.net does not designate 85.214.94.56 as permitted sender) X-PHP-List-Original-Sender: helly@php.net X-Host-Fingerprint: 85.214.94.56 aixcept.net Linux 2.6 Received: from [85.214.94.56] ([85.214.94.56:53807] helo=h1149922.serverkompetenz.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8C/93-22699-7AF1A884 for ; Fri, 25 Jul 2008 14:47:03 -0400 Received: from MBOERGER-ZRH (ip131.fa1-0-1.occ.iinet.com [198.145.32.131]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by h1149922.serverkompetenz.net (Postfix) with ESMTP id 0A3B311EFEA; Fri, 25 Jul 2008 20:46:59 +0200 (CEST) Date: Fri, 25 Jul 2008 20:46:53 +0200 Reply-To: Marcus Boerger X-Priority: 3 (Normal) Message-ID: <1324710133.20080725204653@marcus-boerger.de> To: "troels knak-nielsen" CC: "php-dev List" In-Reply-To: <98b8086f0807250804j2babdfc9y586c4f6beb217b23@mail.gmail.com> References: <98b8086f0807250804j2babdfc9y586c4f6beb217b23@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] [RFC] __toBoolean() From: helly@php.net (Marcus Boerger) Hello troels, Friday, July 25, 2008, 5:04:31 PM, you wrote: > First, apologies if this has been discussed before. I couldn't find > any evidence to suggest that it has, but it kind of surprises me. > As strings aren't objects in PHP, __toString is quite a useful > construct, but it begs the question as to why there aren't > __to for each of the primitive types in PHP? Through > SPL, we have __toArray covered, and I presume that there is no real > value in a __toInteger or __toFloat, but __toBoolean seems as it could > be quite useful, since it would allow an object to be evaluated in a > conditional. Eg. a simple use-case: > class Validation { > protected $errors = array(); > function fail($error) { > $this->errors[] = $error; > } > function __toBoolean() { > return count($this->errors) === 0; > } > } > I wonder if the reason why this haven't been suggest yet is because of: > a) Nobody thought about it before > b) Somebody thought about it, and figured out that it was a bad idea > While this looks pretty simple, I have a hunch that introducing such > behaviour could have far-fetching consequences, as it hooks into PHP's > dynamic typing system. It also have a smell of C++'s ability to > overload operators and the ensuing shooting of feet. On the other > hand, it does allow some nifty tricks, and as it's optional, > presumably people would only use it when it actually makes sense. > Even if this, for some reason, doesn't fit into core PHP, it might be > a candidate for SPL? (Even if the syntax would then be slightly > different) No, there is SPL_Types in pecl which is a good place for type stuff. Actually SPL_Types allows objects that behave as booleans. Other than that we do not want this type of conversion support and it was discussed before and rejected every single time. Best regards, Marcus