Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:39323 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 65105 invoked from network); 25 Jul 2008 15:04:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 25 Jul 2008 15:04:35 -0000 Authentication-Results: pb1.pair.com smtp.mail=troelskn@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=troelskn@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.200.169 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: troelskn@gmail.com X-Host-Fingerprint: 209.85.200.169 wf-out-1314.google.com Received: from [209.85.200.169] ([209.85.200.169:1155] helo=wf-out-1314.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 12/B4-22699-28BE9884 for ; Fri, 25 Jul 2008 11:04:35 -0400 Received: by wf-out-1314.google.com with SMTP id 26so2708413wfd.26 for ; Fri, 25 Jul 2008 08:04:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type:content-transfer-encoding :content-disposition; bh=vfPKcPifz+UQNoCitWArVTHinywRZj7sM1ixJwVS99Y=; b=LZrEHLczqiF1D1jHxDjJLqgR7639MQ6Gj2/s9AFz2KuddvD6L4QQm1pY2hAQLbd4wG rcoAjUKjNt/uYlrJIkJiO/ji7yicZcNPU1EodM7KUafLoTfZN6uR8OBYuhStsc54yQ8D Plcm1P+8NSts66kj6rmfEEClNPL7s5fv6b4mU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type :content-transfer-encoding:content-disposition; b=SCndjRAw0jTUA2n/xDXfgUeDXwMYS8knJsWQtDtDltUwK/Dna54vJAwn9yCO2N6Wti xmu6T4Rz/hCuZxTi3sm+9Kni625wsY06eaUZ1dP2456KjwmwJjwTgi16ZxlXTWto53Bz hNfLzbPnL/FW1o3YdLW1dYGjzVFB+AlpXN0To= Received: by 10.142.109.8 with SMTP id h8mr579506wfc.150.1216998271659; Fri, 25 Jul 2008 08:04:31 -0700 (PDT) Received: by 10.142.110.9 with HTTP; Fri, 25 Jul 2008 08:04:31 -0700 (PDT) Message-ID: <98b8086f0807250804j2babdfc9y586c4f6beb217b23@mail.gmail.com> Date: Fri, 25 Jul 2008 17:04:31 +0200 To: "php-dev List" MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Subject: [RFC] __toBoolean() From: troelskn@gmail.com ("troels knak-nielsen") 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) Have a nice weekend. -- troels