使用来自同一类 PHP 中另一个函数的数据


using data from another function in same class php

对我来说只是有点昏倒。我有一个有效的过程代码,对于某些人来说,我尝试将其部署在 PHP MVC 中,但我需要您的帮助对我来说有点新。请检查我的代码。和正确。提前非常感谢你。

 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(); 从内部输入函数

我会从类的构造函数中的$_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;
  }
}