All I want to be able to do is this:
$s = simplexml_load_file('rss.xml');
foreach ($s->item as $item) {
print htmlentities($item->title . "\n");
}
That's not *all* I want to do, but this is my three-line test case. If
we can figure out a way to make this work, then I'm a happy camper.
I *don't* think it's realisitic to make a person explictly cast
$item->title to a string here. That's not SimpleXML, that's
ConfusingXML, particularly when this works perfectly fine:
print $item->title;
I can't imagine any user in the world understanding why one works and
the other doesn't.
Additionally, I don't think only supporting a print-only __toString()
method is a good thing. That assumes we operate in a CLI context. In a
Web context, people need to call htmlentities() on their data to
ensure their HTML is valid and to prevent XSS attacks. A non-safe lazy
default is asking for trouble.
As to whether this type of magic should be allowed only in C
extensions versus userland classes, is there a fundamental technical
issue or are we only trying to prevent novices from shooting
themselves in the foot?
-adam
On Mon, 1 Dec 2003, Wez Furlong wrote:
> I favour a), if you mean that (string)$com_object will work.
> I'm not so bothered with userspace objects.
>
> b) is just wrong for overloaded objects; if we do some magic
> and either hide an existing __toString() method, or call one
> with the wrong parameters (it might implement one differently
> to the way that we expect), then we end up in trouble.
>
> --Wez.
>
> > I think there are two options:
> > a) Revert the cast operator patch and don't support automatic string
> > conversion by PHP objects (i.e. allowing overloading extensions to support
> > it). But you still shouldn't expect automagical conversions when passing
> > COM objects to functions which accept strings as a parameter.
> > b) Add a __toString() C API function and make $obj->__toString() call
> that.
> > It would mean that your COM example would look like
> > serialize($com_object->__toString()).
> >
> > Personally, I prefer (b) but I think I can live with both.
> > Note: In both cases you won't have automatic conversions.
> >
> > Andi
> >
> >
> >
>
>
--
[EMAIL PROTECTED]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php