我正在嘗試讓某個類正常工作,但出於某種原因,我無法找到我在該類中構建的變量。下面是類代碼:類文件的URL(cls_question_object.php)PHP,身份不明的變量
class question_object{
// the id to the question
public $i_id = "";
// the question string
public $s_qString = "";
// the question type
public $s_qType = "";
/* the array of answer strings
*/
public $a_qAnswerStrings = array();
public $a_qAnswerSet = array("id"=>"","string"=>"");
}
這裏是我與我的測試類的代碼:文件URL(test_question_object.php)
include("cls_question_object.php");
/* - test for class question object -
*/
$cls_obj = new question_object;
$cls_obj->$i_id = "1";
$cls_obj->$s_qString = "test question string";
$cls_obj->$s_qType = "fib";
$cls_obj->$$a_qAnswerStrings[0]['id'] = "0";
$cls_obj->$$a_qAnswerStrings[0]['string'] = "test answer string";
print_r($cls_obj);
這裏是我得到的錯誤:
Notice: Undefined variable: i_id in C:\wamp\www\Exam Creator\test_question_object.php on line 9
請不要用之類的東西'i_'爲整數或's_'字符串前綴的變量。這是一個令人難以置信的過時的做法,並且在PHP中變量的類型可以並且將改變以適應其使用的價值特別小。你只會以這種方式產生醜陋,難以維護的代碼。 – meagar 2011-03-21 02:22:20