note 94633 deleted from language.variables.variable by danbrown

From: Date: Mon, 16 Nov 2009 13:54:24 +0000
Subject: note 94633 deleted from language.variables.variable by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-162951@lists.php.net to get a copy of this message
Note Submitter: al at o3strategies dot com ---- This is an extremely handy use for variable variables especially when dealing with direct data modeling. This will allow you to automatically set object properties based on a query result. When new fields are added to the table, the class will receive these properties automatically. This is great for maintaining user data or other large tables. Your property names will be bound to your column name in the database, making maintenance worry free. This method uses $this->{$var} for the variable variable creation. class testTableData() { function testTableData(){ // testTable includes the columns: name, user, date $query = "SELECT * FROM testTable"; $result = mysql_query($query) or die (mysql_error()); $row = mysql_fetch_array($result); foreach ($row as $var => $key) { $this->{$var} = $key; } } } // Access table properties $table = new testTableData(); echo $table->name; echo $table->user; echo $table->date;

« previous php.notes (#162951) next »