如何将变量从1个函数传递给另一个函数

i want to pass variable from one function in codeigniter to another they are sharing the same class. they are part of form.

class C_Reservation extends CI_Controller {

 private $params = array();
 private $post;
 public function index()
     {
          $this->params['post'] = array(
                'name' => '',
                'last_name'=>''
          );

          $this->load->view('common/default_header',$this->params);
          $this->load->view('reservation/index',$this->params);
          $this->load->view('common/default_footer');

     }

        public function add(){
            $error =false;
            $error_list =array();

            session_start();
            $this->load->library('InputProcess');
            $this->load->library('session');

            if($this->input->server('REQUEST_METHOD')!="POST"){
                show_404("Formularz dodawania obiektu - brak danych POST");
            }

            //filtrowanie danych

            $this->post=array(
                'name'=>$this->input->post('name'),
                'last_name'=>$this->input->post('last_name')
            );
            echo $this->post['name']; // this place is showing name from form
            $this->load->view('common/default_header',$this->params);
            $this->load->view('reservation/complete',$this->params);
            $this->load->view('common/default_footer');

        }

     public function export_pdf(){
         session_start();

         $html=$this->load->view('pdf/index', $this->post,false);
         $this->load->library('m_pdf');
        $pdf = $this->m_pdf->load();
        $pdf->WriteHTML($html);
        $pdf->Output();
     }
}

add function is unfinished it just need to "save" variable that i can acces it from function export_pdf anyone ?

Your question is a bit confusing. The reason is confusing is because you are not explaining the process of how the application flows. Taking in consideration all the comments, I believe the flow is like this:

When a user adds a reservation to the system:

  • a POST request is sent to the add() function
  • the function makes sure it is a POST request
  • the function sets the POST values to class variable ($this->post)
  • the function checks if the user wants a PDF output
  • If yes, then should run the export_pdf() to create a pdf
  • If no, then the completed html should be returned

If the above is your desired flow, then problem lies in your add() and export_pdf() functions. The function needs to make sure the export_pdf() is called, and export_pdf() needs to return the data, but currently nothing is running that function.

I would rewrite both functions like this:

public function add(){
    $error = false;
    $error_list = array();

    session_start();
    $this->load->library('InputProcess');
    $this->load->library('session');

    if($this->input->server('REQUEST_METHOD')!="POST"){
        show_404("Formularz dodawania obiektu - brak danych POST");
    }

    //filtrowanie danych

    $this->post = $this->input->post();
    if ($this->post['user_needs_pdf'] == TRUE) {
        $this->export_pdf();
    } else {
        $this->load->view('common/default_header',$this->params);
        $this->load->view('reservation/complete',$this->params);
        $this->load->view('common/default_footer');
    }
}

public function export_pdf(){
    $html = $this->load->view('pdf/index', $this->post, false);
    $this->load->library('m_pdf');
    $pdf = $this->m_pdf->load();
    $pdf->WriteHTML($html);
    $pdf->Output();
 }

If your are not calling both function at the same time, you have to store value in cookie/session, then only one can get values from one method to another. Remember HTTP is stateless state.

other wise simple $this->export_pdf($f_name, $l_name); retrieve it with function export_pdf($f_name, $l_name){....} or in your case $this->export_pdf(); and in function export_pdf(){$f_name=$this->post['f_name']; $l_name=$this->post['l_name'];}