從以下html
,從文本字段中的數據是由action_script.php
服務:當我在類的構造函數中定義它時,爲什麼會得到未定義變量的錯誤?
<form method='post' action='action_script.php'>
<input type='text' name='text_field' id='text_field' />
<input type='submit' value='submit' />
</form>
action_script.php
包含以下代碼:
<?php
class Tester {
private $text_field;
public function __construct() {
$text_field = $_POST['text_field'];
}
public function print_data() {
echo $text_field; # LINE NUMBER 10
}
}
$obj = new Tester();
$obj->print_data();
我嘗試打印數據發送從html in action_script.php
但我得到以下警告/錯誤:
Notice: Undefined variable: text_field in E:\Installed_Apps\xampp\htdocs\php\action_script.php on line 10
這是爲什麼?
您正在使用的變量可能未被設置爲構造函數中的值。 – William 2013-03-15 09:43:34