frankx: Frage zu Zend_Form und echo "new Zend_Form()"

Beitrag lesen

Hellihello df,

echo $begrüßung;

Anschließend wäre eine Einführung in die SPL angebracht. Auch damit kann man einige Magie zu Klassen hinzufügen, wie beispielsweise Array-Zugriff und Iteration über Objekte.

<?php
/*** create a new iterator object ***/
$it = new DirectoryIterator('./');

/*** loop directly over the object /
while($it->valid())
        {
        /
check if value is a directory /
        if($it->isDir())
                {
                /
echo the key and current value /
                echo $it->key().' -- '.$it->current().'<br />';
                }
        /
move to the next iteration ***/
        $it->next();
        }
?>


> >   
> > fein das, hatte mir bisher noch niemand gesagt, dass das alles auch so geht...; oder ich habs bisher nicht kapiert oder zwar gesehen aber irgendwie nich abgespeichert. so ein aha-erlebnis hatte ich vor einiger zeit mal mit der alternativen syntax (da dacht ich noch, es bräuchte eine extra templatesprach.  
>   
> So sollst du das nicht anwenden. Das Iterator-Interface definiert die Methoden valid(), key(), current(), next(), und rewind(), damit eine Klasse die für ein foreach notwendigen Informationen bereitstellt. Diese Methoden sind nicht zum Aufruf durch den Anwender gedacht (im Gegensatz zu den isXxx()- und getXxx()-Methoden im Falle des DirectoryIterators).  
  
found in <http://www.phpro.org/tutorials/Introduction-to-SPL.html#3>.  
  
gefolgt vom Kommentar: "So, now we have a list of directories within the path. But we could have done this in a more Object Oriented way that would allow us more flexibility. We begin by extending the DirectoryIterator class."  
~~~php
  
<?php  
/*** class definition to extend Directory Iterator class ***/  
class DirectoryReader extends DirectoryIterator {  
  
// constructor.. duh!  
function __construct($path){  
  /*** pass the $path off to the parent class constructor ***/  
  parent::__construct($path);  
}  
  
/*** return the current filename ***/  
function current(){  
  return parent::getFileName();  
}  
  
/*** members are only valid if they are a directory ***/  
function valid(){  
  if(parent::valid())  
    {  
    if (!parent::isDir())  
        {  
        parent::next();  
        return $this->valid();  
        }  
    return TRUE;  
    }  
  return FALSE;  
}  
  
} // end class  
  
try{  
/*** a new iterator object ***/  
$it = new DirectoryReader('./');  
/*** loop over the object if valid ***/  
while($it->valid())  
    {  
    /*** echo the current object member ***/  
    echo $it->current().'<br />';  
    /*** advance the internal pointer ***/  
    $it->next();  
    }  
}  
  
catch(Exception $e){  
    echo 'No files Found!<br />';  
}  
  
?>  

"The code above demonstrates how we can overload the Iterator methods to reduce the logic within the user code. This creates great opportunities to make portable classes for re-use, thus reducing user code and speeding up development time. Once again the use of Exceptions shows how easy it can be to catch errors and deal with them. We could, of course, use the isFile() method in place of isDir() to show only files. The possibilities are endless. "
 Anwenden sollst du ein foreach auf ein Objekt dieser Klasse:

foreach (new DirectoryIterator('test') as $entry) {

printf("%s ist ein%s\n",
    $entry,
    $entry->isDir() ? ' Verzeichnis' : 'e Datei');
}


>   
> $entry ist übrigens ebenfalls ein DirectoryIterator-Objekt, über das man im Falle eines Verzeichnisses weiter iterieren kann.  
>   
>   
> echo "$verabschiedung $name";  
  
  
Dank und Gruß,  
  
[frankx](http://community.de.selfhtml.org/visitenkarten/view.php?key=82)

-- 
[tryin to](http://sauer-ernst.de) [multitain](http://multitain.de)  - Globus = Planet != Welt