Metoda __toString() służy do konwersji obiektu na typ string. Jeśli wygenerowane klasy wzbogacisz o metody __toString(), to będziesz mógł stosować obiekty jako parametry instrukcji echo, np.:
$poeta = PoetaPeer::retrieveByPK(3);
echo $poeta;
foreach ($poeta->getWierszs() as $wiersz) {
echo $wiersz;
}
Instrukcje:
echo $poeta; echo $wiersz;
będą działały poprawnie, pod warunkiem, że w klasach Poeta oraz Wiersz dodasz metody __toString():
class Poeta extends BasePoeta {
function __toString()
{
return $this->getImie() . ' ' .
$this->getNazwisko();
}
}
class Wiersz extends BaseWiersz {
function __toString()
{
return $this->getTytul();
}
}