从以下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