Re: PHPTAL / HTML_Template_PHPTAL (0.4.7) / GetText / Types
| From: | laurent bedubourg | Date: | Sat, 22 Mar 2003 16:09:50 +0000 |
| Subject: | Re: PHPTAL / HTML_Template_PHPTAL (0.4.7) / GetText / Types | ||
| References: | 1 2 3 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-14550@lists.php.net to get a copy of this message | ||
Le Fri, 21 Mar 2003 01:41:46 +0000, Richard Wall a écrit :
>
> I'm too tired to follow it at the moment, but this looks like it might
> be interesting (linked from the php weekly update)...
> http://marcus-boerger.de/php/ext/spl/spl.phps
> ...a php5 iterator interface which works with php's foreach and for loops.
>
Very interesting, i'm just sorry it's for php5 and look like an extension
that won't be installed on every systems :(
> Another thing I wanted to ask you about is whether you have had any
> success using the iterator in a for loop rather than a while loop.
Absolutely not :) in fact now yes
The solution (remind me c++ in some ways) solution whould be to use
'for' like what follows :
for ($i = $list->iterator(); ! $i->end(); $i->next())
{
// stuff with $i->value()
}
For example, with an iterator over an array(1,2,3) :
element 0 : iterator::end() false iterator::hasNext() true
element 1 : iterator::end() false iterator::hasNext() true
element 2 : iterator::end() false iterator::hasNext() false
element 3 : iterator::end() true
class ...
{
....
function end()
{
return $this->_index >= count($this->_array);
}
}
That work for my iterator implementation (the last element was skipped
with it).
> - although toString() method gets called when using tal:content etc it
> doesn't get called when using alternative ${path/to/object} syntax.
On my way :) (understand on my todo)
> Also, I wondered whether it might be better to make default call to eg
> 'valueOf()'.
>
> In Javascript toString() is used to give a textual view of
> the objects structure, whereas valueOf() returns a simple value.
> Wouldn't you say that 'toString()' is more like the Python __repr__()
> method which in the case of an array(1,2,3) would return eg [1, 2, 3],
> valueOf() might return 6 or 123. What do you think?
Well, as the template system outputs string, i'm not sure it should call a
valueOf() method.
(in java : Integer.valueOf("10") returns an Integer object holding 10)
In fact, toString() seems more 'understandable' for non aware developpers,
they'll think : "how can i output my object representation in my template
? Ah yes toString()".
If we give them a valueOf(), java developers will be lost and the
mind association "object/template/printing" will occurs with difficulties
(still IMHO).
Actually i don't think of toString() as a way to dump data but a way
to customize a string appearance of the object (example: calling
another template inside toString() ).
For common types like arrays, hashtables, i think that we can stay with a
"[1, 2, 3]".
Deciding that the printing of an array should be '6' or '123" is quite
anoying too me :)
Will this be the number of elements, the first element, the sqrt of the
sum of all elements ? Template designers will 'never' understand why a 6
is output in their template result, and if the 6 change its value each
time the page is reloaded ?
In fact, we never print containers objects, we loop over them (unless
debug).
> I think standard types and an iterator interface should be added to
> pear. I use something similar to your iterator in my IVM package (also
> included in the zip file). I'd rather use method name 'getIterator()'
> than 'iterator()' in iterable objects.
I think so, standard types should make PEAR coders life easier.
The best point in using iterators is we can loop over database
results, array, hashtables, files, any collection of data, produced
by other packages without making wrappers everywhere. An abstract loop
over any kind of iterator is surely better than 200 tests to change the
loop behaviour depending of its input type.
> I'll try and have a look at doing some docbook docs over the weekend
I've started a big document based on my text one. After some editors try
(abiword, open office) and because i didn't want to see docbook tags while
writing, i failed into kword which allow some basic sgml export, now i'm
going with lyx which seems to support SGML (docbook DTDs) with much more
features and stability :)
Please don't begin a document now, (examples are fine) i'll send you
the result of my work as soon as possible (in lyx and docbook format
if Lyx/latex do their job as expected).
I'll give you my icq number just in case
(send separately unless i forget:)
--
Laurent Bédubourg
laurent.bedubourg@free.fr