从以下内容可以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>
Run Code Online (Sandbox Code Playgroud)
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();
Run Code Online (Sandbox Code Playgroud)
我尝试打印从html 发送的数据,action_script.php但我得到以下警告/错误:
Notice: Undefined variable: text_field in E:\Installed_Apps\xampp\htdocs\php\action_script.php on line 10
Run Code Online (Sandbox Code Playgroud)
这是为什么 ?
在课堂内,您必须使用$this->,例如,引用您的成员属性
<?php
class Tester {
private $text_field;
public function __construct() {
$this->text_field = $_POST['text_field'];
}
public function print_data() {
echo $this->text_field; # LINE NUMBER 10
}
}
$obj = new Tester();
$obj->print_data();
Run Code Online (Sandbox Code Playgroud)
您还应该$_POST['text_field']在使用之前检查是否已设置
我正在使用该$this语句,但是仍然存在相同的问题,然后我发现被调用的变量一定不能带有$符号,就像上面提到的一些用户一样。
例如,它一定不能这样:
$this->$text_field;
Run Code Online (Sandbox Code Playgroud)
这是正确的方法:
$this->text_field;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2858 次 |
| 最近记录: |