Php 如何访问类中的公共变量。

Php 如何访问类中的公共变量。,php,codeigniter,Php,Codeigniter,我在访问codeigniter中控制器类内的方法中的公共声明变量时遇到问题 这就是我声明变量的地方 Class Main_controller extends CI_Controller{ public $firstname = ""; public $middlename = ""; public $lastname = ""; public $suffix = ""; public $age = "";

我在访问codeigniter中控制器类内的方法中的公共声明变量时遇到问题

这就是我声明变量的地方

Class Main_controller extends CI_Controller{




        public $firstname = "";
        public $middlename = "";
        public $lastname = "";
        public $suffix = "";
        public $age = "";
        public $city = "";
        public $street = "";
        public $house = "";
我想通过这个方法访问它,但它告诉我变量没有声明

public function enroll_step_2(){

        $this->load->library('session');
        $this->load->model('login_model');
        $this->form_validation->set_rules('personalinfo_city', 'city', 'required');
        $this->form_validation->set_rules('personalinfo_street', 'street', 'required');
        $this->form_validation->set_rules('personalinfo_house_no', 'house', 'required');
        $this->load->helper(array('form', 'url'));

        if($this->form_validation->run() == FALSE){

                $this->load->view('enroll_step_2.php');             

        }
        else
        {



                $this->$city = $this->input->post('personalinfo_city');
                $this->$street = $this->input->post('personalinfo_street');
                $this->$house = $this->input->post('personalinfo_house_no');

                $data['firstname'] = $this->$firstname;
                $data['middlename'] = $this->$middlename;
                $data['lastname'] = $this->$lastname;
                $data['suffix'] = $this->$suffix;
                $data['age'] = $this->$age;
                $data['city'] = $this->$city;
                $data['street'] = $this->$street;
                $data['house'] = $this->$house;



                $this->load->view('welcome_user',$data);


        }

所有这些都在同一个类中。

将此
$this->$city
更改为
$this->city
,其他所有的都一样。不需要额外的
$


我可能会补充说,您使用类变量的方式很奇怪,因为当页面被更改/刷新时,它不会持续存在。

谢谢,我只是尝试一下,而是将变量存储在会话中。正如我所说,它不会持续存在,所以如果您需要它们,您确实应该使用会话变量。