Friday, December 03, 2004

[tech] PHP dynamically created class from DB query

On page 300 of PHP Cookbook (Sklar and Trachtenberg) show the recipe for using Smarty Template. What I find that looks really interesting is this:

$r = mysql_query('SELECT * FROM cheese');
while ($ob = mysql_fetch_object($r)) {
  $ob->price = sprintf('$%.02f', $ob->price);
  $results[] = $ob;

Wow. This mysql_fetch_object() function just dynamically created a class and instantiated the object. That, is pretty nice. I like.