我有以下类,它有很多私有变量。
class plantOfTheMonth {
//Declare which centre(s) are being used
private $centre = "";
//Declare the attributes of the current Plant Of The Month
private $name = "";
private $latinName = "";
private $image = "";
private $imageAlt = "";
private $imageLink = "";
private $strapLine = "";
private $description = "";
private $colour = "";
private $centres = "";
//Declare variables for error handling
private $issue = "";
private $issueCode = "";
public function __construct() {
}
public function returnAttributes() {
$list = ""; //Set an Empty List
foreach($this as $key => $value) {
decodeText($value); //decode performs a stripslashes()
$$key = $value; //Use a variable variable and assign a value to it
$list .= "'".$key."', "; //add it to the list for the compress()
}
$list .= substr($list, 0, -2); //Take the final ", " off
return compact($list); //return the list of variables as an array
}
}
我想将所有属性作为带有它们的值的变量返回,以便我可以预先填写表单字段。我有一个填充所有属性的数据库查询(经测试证明有效)。在我之前 OO 的日子里,我从数据库中检索信息,将其放入变量中,然后使用 compress() 发送和 extract() 来获取所有变量。这是可行的吗,就像我课堂上的 returnAttributes() 方法一样?