如何访问由另一个函数设置的PHP类中的变量?共点火器 我在PHP中使用CODEIGITER框架有这个代码,我不能让我的头在这个代码中的类变量周围看起来完全不同于C++。

如何访问由另一个函数设置的PHP类中的变量?共点火器 我在PHP中使用CODEIGITER框架有这个代码,我不能让我的头在这个代码中的类变量周围看起来完全不同于C++。,php,class,codeigniter,variables,attributes,Php,Class,Codeigniter,Variables,Attributes,我想知道如何将一个类(函数)方法中的局部变量转换为另一个类方法 但是不要将它们作为变量传递,因为我必须使用重定向函数,它不能接受变量 我想要访问的变量是$record\u id,我尝试将其公开,但etc不喜欢它 类提交扩展CI_控制器 { 这是我希望访问$record\u id public function success() { $page['page'] = 'success'; $page['record'] = $record_id; $this->load->view(

我想知道如何将一个类(函数)方法中的局部变量转换为另一个类方法

但是不要将它们作为变量传递,因为我必须使用重定向函数,它不能接受变量

我想要访问的变量是$record\u id,我尝试将其公开,但etc不喜欢它

类提交扩展CI_控制器 {

这是我希望访问$record\u id

public function success()
{

$page['page'] = 'success';
$page['record'] = $record_id;
$this->load->view('template', $page );


}
记住-我不能将其作为变量传递给其他函数,因为我需要使用重定向,这样我的URL就不会出错。谢谢


干杯!

使用codeigniter的小功能Flashdata,它允许您在请求之间临时存储数据

所以你的代码是

function send data{
$this->session->set_flashdata('recordid', $recordid);
}

function success{
 $recordid =  $this->session->flashdata('recordid');
}

明白了吗?

它不会在success()函数()上得到
$this->record\u id==false
,因为我假设在重定向时,我们再次重新加载控制器,但由于类的新实例,它将转到success()方法,其中
$this->record\u id
放置了一个false值。
class Submit extends CI_Controller {

    private $record_id=false;

    public function __construct(){
         if(isset($_SESSION['record_id'])){
              $this->record_id = $_SESSION['record_id'];
         }
    }

    public function send_data(){
        $this->record_id = $this->submit_model->create_record($completedstaffrows, $completedeventrows);
        $_SESSION['record_id'] = $this->record_id;
        if ($this->record_id == FALSE){
            echo "Failed to add to database";
        }
        else{
            redirect('submit/success');
        }
        return;
    }
    public function success(){

         $page['page'] = 'success';
         $page['record'] = $this->record_id;
         $this->load->view('template', $page );
    }
}
class Submit extends CI_Controller {

    private $record_id=false;

    public function __construct(){
         if(isset($_SESSION['record_id'])){
              $this->record_id = $_SESSION['record_id'];
         }
    }

    public function send_data(){
        $this->record_id = $this->submit_model->create_record($completedstaffrows, $completedeventrows);
        $_SESSION['record_id'] = $this->record_id;
        if ($this->record_id == FALSE){
            echo "Failed to add to database";
        }
        else{
            redirect('submit/success');
        }
        return;
    }
    public function success(){

         $page['page'] = 'success';
         $page['record'] = $this->record_id;
         $this->load->view('template', $page );
    }
}