note 94634 deleted from language.variables.variable by cmb
| From: | cmb@php.net | Date: | Sun, 12 Jan 2020 08:33:11 +0000 |
| Subject: | note 94634 deleted from language.variables.variable by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213865@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.
<?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;
?>