There might be some places in the engine which aren't covered but it's best for the SimpleXML authors to check this out first because in general it should work.
At 13:37 27/02/2004 -0500, Adam Maccabee Trachtenberg wrote:
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('<root a="123" />'); 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
-- [EMAIL PROTECTED] author of o'reilly's php cookbook avoid the holiday rush, buy your copy today!
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php