在同一个php类中使用来自另一个函数的数据
我有点晕倒了。 我有一个过程代码,它正在工作,我尝试在php MVC中部署它,但我需要你的帮助,这对我来说是一种新的东西。 请检查我的代码。而且正确。 提前非常感谢你在同一个php类中使用来自另一个函数的数据,php,model-view-controller,Php,Model View Controller,我有点晕倒了。 我有一个过程代码,它正在工作,我尝试在php MVC中部署它,但我需要你的帮助,这对我来说是一种新的东西。 请检查我的代码。而且正确。 提前非常感谢你 class Insert extends Controller { var $gender; var $ageR; function __construct() { parent::__construct();
class Insert extends Controller
{
var $gender;
var $ageR;
function __construct()
{
parent::__construct();
}
function xhrInsert()
{
// I want to be able to reuse the value of Post in Function below
$gender = $_POST ['gender'];
$ageR = explode ( ',', $_POST ['age'] );
$this->model->xhrInsert($gender,$ageR[0],$ageR[1]);
}
function getReiseType()
{
// I need the value from $gender in function xhrInsert() here. Because i dont want to $gender = $_POST['gender']; here anymore.
$this->model->getReiseType();
}
}
使用self::FunctionName();要从中输入函数,我将从类的构造函数中的
$\u POST
中提取所需的数据,并使用类变量存储它们
class Insert extends Controller {
var $gender;
var $ageR;
function __construct() {
parent::__construct();
$this->gender = $_POST['gender'];
$this->ageR = explode(',', $_POST['age']);
}
function xhrInsert() {
$this->model->xhrInsert($this->gender,$this->ageR[0],$this->ageR[1]);
}
function getReiseType() {
$this->model->getReiseType();
// Access class properties as you like
echo $this->gender;
}
}
您想使用
$gender、$ageR[0]、$ageR[1]
对吗?您应该能够在同一实例中正常使用它们($gender
,$ageR
)。它们都被标记为protected.Thanx,我这样做了,但是在getReiseType函数中得到了一个空值。所以我们的想法是错误的。但是你能告诉我为什么我从secound函数中得到空值吗?你试过echo$this->gender方法\uuu construct()
中的code>?如果$\u POST['gender']
中有数据,则上述代码将起作用。请验证是否设置了$\u POST['gender']
。函数xhrInsert()正在将数据正确地发送到数据库。但是我想在函数getReiseType()中将post数据重新用于其他目的,而无需初始化post数据2次或更长时间使用$this->model->getReiseType($this->gender,$this->ageR)方法中的代码>和处理数据getReiseType()
。对我来说很有趣,我做了同样的事情,但它仍然得到空值输出。我调试。还是英国电信。