duanqie8549
2015-11-13 09:07
浏览 49
已采纳

如何在同一个类-Codeigniter的另一个函数中使用函数的变量

I want to assign the value of $Id from firstFunction to the variable $temp in secondFunction.

public function firstFunction()
{
    $Id = $_POST['quizID_click'];
}


public function secondFunction()
{
    $temp = ------;
}

Please let me know how to access $Id in secondFunction

图片转代码服务由CSDN问答提供 功能建议

我想从 firstFunction $ Id 的值 >到 secondFunction 中的变量 $ temp

  public function firstFunction()
 {
 $ Id =  $ _POST ['quizID_click']; 
} 
 
 
公共函数secondFunction()
 {
 $ temp = ------; 
} 
   \  n 
 

请告诉我如何访问 secondFunction 中的 $ Id

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • duanlu8613 2015-11-13 09:14
    已采纳

    If i understand correctly you want to pass only a variable inside the Controller functions, You can do it by throwing a flash like this:

        public function firstFunction()
        {
            $this->load->library('session');
            $Id = $_POST['quizID_click'];
            $this->session->set_flashdata('item', $Id);
        }
    
    
        public function secondFunction()
        {
            $myVar = $this->session->flashdata('item');
        }
    

    Basically you temporary save the variable value as flashdata giving it a 'item' value 'item' can be anything. then the second controller looks for this flashdata and asigns it to $myVar

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • drfls28608 2015-11-13 09:10

    Functions create local variables which are destroyed when the function completed. To maintain the variable you need, you'll have to return it from the function and then assign it to a variable.

    I can see that you are using classes, so you'll need to access the function like a method. This should work:

    public function firstFunction()
    {
        $Id = $_POST['quizID_click'];
        return $Id;
    }
    
    
    public function secondFunction()
    {
        $temp = $this->firstFunction();
    }
    
    评论
    解决 无用
    打赏 举报
  • doushi8599 2015-11-13 09:15

    Create a public variables of your class so that you can access the variables in all methods.

    class myClass{
        public $id;
    
        public function first(){
            $this->id = $this->input->post('quizID_click');
        }
    
        public function second(){
            $id = $this->id;
        }
    }
    

    But that depends on what you are tying to accomplish? Please elaborate what you are trying to do.

    评论
    解决 无用
    打赏 举报
  • dqnk57224 2015-11-13 09:15

    To expand on my comment, you could create your variable as a property of your class.. it's been a while since used CI so forgive me, classes are classes though :)

    class whatever
    {
        public $id;
    
        public function firstFunction($postData)
        {
             $this->id = $postData['quizID_click'];
        }
    
        public function secondFunction()
        {
            $temp = $this->id;
        }
    }
    

    Hope that gets you going

    评论
    解决 无用
    打赏 举报
  • doudang8824 2015-11-16 10:23
    public function firstFunction()
    {
        $Id = $_POST['quizID_click'];
        $this->seconFunction($id);
    }
    
    
     public function secondFunction($id)
     {
         $temp = $id;
     }
    

    This is how to pass variables among functions.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题