...  9   10   11   12   13   14   15   16 

Propel. Porady

13. Porada #13: Metoda __toString()

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();
  }
}
...  9   10   11   12   13   14   15   16