On Thu, 15 Jan 2004, Rob Richards wrote:

> Ignore the user space issue for right now as I dont go into that at all. I
> dont see iterators as being worthless, however there is a behavior clash
> between the iterators and the arrays.

I really like Iterators, so I'd like to see this ironed out. Based on
what I've seen from Marcus at ApacheCon you can do really cool stuff
with them and I don't think there's any reason to delete them if they
can be implemented consistently alongside the rest of the
extension. (Which I'm sure they can be.)

> When fetching properties returning arrays for multiple elements and
> returning a single sxe object for single elements (as the single sxe object
> will iterate the children of the returned object). A foreach will give
> different results depending upon which is returned.
> $a = $foo->a;
> foreach($a->b as $b) {
>  ...
> }
>
> imo, the behavior should be the same regardless of what is returned. This
> pretty much means generalizing things to use either arrays or iterators.

This is one of my problems because even if you know a document's
Schema, you can still run into instances where a node may have 0, 1,
or more subelements of the same name.

I know this has been discussed before. (I think I remember George
being involved, but I can't remember who else.)

The current solution is to do:

if (is_set($foo->a)) {
  if (is_array($foo->a)) {
    foreach($foo->a as $a) {
        // blah with $a
    }
  } else {
    // blah with $foo->a
  }
}

It would be a "Real Good Thing" (tm), if that could be turned into this:

foreach($foo->a as $a) {
  // blah $a
}

If this can be done using Iterators, I see it as a strongly compelling
reason to use them. I think this is a clear-cut win in terms of
programming simplicity (and I should know since I've got over 2,000
lines of code that's either the little dance above or a variation of it).

> However to implement something like above as well as to implement iterators
> in sxe at all, they are going to need to point to the node proxies rather
> than to the libxml nodes themselves otherwise it can be made to segfault (as
> is the case today):
> $foo =
> simplexml_load_string('<foo><a><b><b1>b1val</b1><b2>b2val</b2></b></a></foo>
> ');
> $a = $foo->a;
> foreach($a as $b) {
>  foreach($b as $subb) {
>   print $subb."\n";
>   unset($foo->a);
>  }
> }

Nasty crashes are bad. :)

-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

Reply via email to