note 94634 added to language.variables.variable
| From: | al at o3strategies dot com | Date: | Mon, 16 Nov 2009 02:01:41 +0000 |
| Subject: | note 94634 added to language.variables.variable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162941@lists.php.net to get a copy of this message | ||
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.
<?php
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;
?>
----
Server IP: 69.147.83.197
Probable Submitter: 174.98.203.168
----
Manual Page -- http://www.php.net/manual/en/language.variables.variable.php
Edit -- https://master.php.net/note/edit/94634
Del: integrated -- https://master.php.net/note/delete/94634/integrated
Del: useless -- https://master.php.net/note/delete/94634/useless
Del: bad code -- https://master.php.net/note/delete/94634/bad+code
Del: spam -- https://master.php.net/note/delete/94634/spam
Del: non-english -- https://master.php.net/note/delete/94634/non-english
Del: in docs -- https://master.php.net/note/delete/94634/in+docs
Del: other reasons-- https://master.php.net/note/delete/94634
Reject -- https://master.php.net/note/reject/94634
Search -- https://master.php.net/manage/user-notes.php