Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:45416 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 41347 invoked from network); 27 Aug 2009 08:15:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Aug 2009 08:15:25 -0000 Received: from [127.0.0.1] ([127.0.0.1:10388]) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ECSTREAM id BE/B8-09105-D90469A4 for ; Thu, 27 Aug 2009 04:15:25 -0400 Authentication-Results: pb1.pair.com header.from=vanhanit@arcada.fi; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=vanhanit@arcada.fi; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain arcada.fi designates 193.167.33.246 as permitted sender) X-PHP-List-Original-Sender: vanhanit@arcada.fi X-Host-Fingerprint: 193.167.33.246 hipper.arcada.fi Received: from [193.167.33.246] ([193.167.33.246:50973] helo=hipper.arcada.fi) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B4/88-09105-C4D369A4 for ; Thu, 27 Aug 2009 04:01:19 -0400 Received: from [193.167.34.30] (dhcp34-30.arcada.fi [193.167.34.30]) (authenticated bits=0) by hipper.arcada.fi (8.13.8/8.13.8) with ESMTP id n7R81DiE020400 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Thu, 27 Aug 2009 11:01:14 +0300 Message-ID: <4A963D41.1010508@arcada.fi> Date: Thu, 27 Aug 2009 11:01:05 +0300 User-Agent: Thunderbird 2.0.0.23 (Windows/20090812) MIME-Version: 1.0 To: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Typecasting objects with __toString function From: vanhanit@arcada.fi (Thomas Vanhaniemi) Hi! I was testing typecasting of objects to different types. Discovered that if you try to cast an object to other types except string you get a PHP notice that it isn't allowed. I'm using PHP 5.3, and it would be nice if PHP would call my __toString function BEFORE trying to typecast. This way one can, e.g. have an own string class and use the variables in the same way as you would use an ordinary string variable. Some code: value = (string)$value; } public function __toString() { return (string)$this->value; } } $test1 = new CastTest('10'); $test2 = new CastTest(11); echo "Test 1 [String: $test1, Int: " . (int)$test1 . ", Float: " . (float)$test1 . "]\n"; echo "Test 2 [String: $test2, Int: " . (int)$test2 . ", Float: " . (float)$test2 . "]\n"; ?> Result (excluding the PHP Notices): Test 1 [String: 10, Int: 1, Float: 1] Test 2 [String: 11, Int: 1, Float: 1] Nice to have result (no notices): Test 1 [String: 10, Int: 10, Float: 10] Test 2 [String: 11, Int: 11, Float: 11] Can this be fixed, or can a more generic magic function be added that provides this functionality? Regards, Thomas Vanhaniemi