Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:8239 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 98176 invoked by uid 1010); 27 Feb 2004 18:37:15 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 98152 invoked from network); 27 Feb 2004 18:37:15 -0000 Received: from unknown (HELO miranda.org) (209.58.150.153) by pb1.pair.com with SMTP; 27 Feb 2004 18:37:15 -0000 Received: (qmail 20773 invoked by uid 546); 27 Feb 2004 18:37:15 -0000 Received: from localhost (sendmail-bs@127.0.0.1) by localhost with SMTP; 27 Feb 2004 18:37:15 -0000 Date: Fri, 27 Feb 2004 13:37:15 -0500 (EST) X-X-Sender: adam@miranda.org To: internals@lists.php.net Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Subject: SimpleXML & Casting to String From: adam@trachtenberg.com (Adam Maccabee Trachtenberg) I know we discussed this already, but after seeing a couple of bug reports about SimpleXML, I'm worried our decision only makes sense to us and not to regular users. :) Specifically, since elements and attibutes look like strings, people expect them to act like strings. But since they're not objects instead of strings, they're completely buffled as how to handle them. Here are two examples that have come through the bug report system in the last day: $sxe = simplexml_load_string(''); if ($sxe['a'] == '123') { // do something } And: $xml = simplexml_load_string(/* some valid XML string that I'm not going to cut and paste here */); foreach($xml->user as $user){ if (utf8_decode($user->login) == $login && utf8_decode($user->password) == $password) { // valid users } } Both seem like they should work, but neither do. In the first example, we're comparing an object with a string. Even though $sxe['a']->__toString() == 'a', the comparison fails. (Well, you can't actually do that, but you know what I mean.) In the second example, utf8_decode() expects a string and not an object and we again we don't autoconvert. The problems can be solved by explicitly casting the object to a string, but since you rarely need to cast elsewhere in PHP, I don't think anyone thinks of it as a necessary step. Originally, I proposed that PHP autoconvert an object to a string whenever the object has a __toString() and it's necessary to treat the variable as a string. In the first example, since we're comparing an object to a string, we would cast down the object to enable the comparison. This would work just like 1 == '1'. In the second case, since the function expects a string, we'd also do the cast. However, Andi (and others) raised some valid issues about edge cases and other potential engine problems. Is there anything we can do to help out people so that SimpleXML works as they expect, but doesn't have the potential to unleash hell on PHP and the bug system? Maybe it makes sense to have SimpleXML leaf nodes return as strings instead of SimpleXML objects? Or does this merely substitute one set of problems for another? (E.g. this breaks iteratation, what happens when there's multiple leaves, etc.) I don't know what the right answer is, but I feel that the current solution isn't perfect. It may end up to be the best possible method, but I'm not yet convinced it is. -adam -- adam@trachtenberg.com author of o'reilly's php cookbook avoid the holiday rush, buy your copy today!