我有一个数组,希望它的值为"public $somevariable"。下面是我的函数代码:
public function __construct(){
global $database;
$result = $database->query("SHOW COLUMNS FROM ".self::$tabel_name."");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
$attributes_var = array();
while ($row = $database->fetch_array($result)) {
$attributes_var[] = $row[0];
}
}
foreach($attributes_var as $key)
{
public $$key;
}
}
但是"public $$key"出现了语法错误。我想把动态生成的变量用作公共变量,并在类外部使用它们。
有什么建议吗?
谢谢!