代码点火器如何访问类内的公共变量。

时间:2018-08-02 06:12:08

标签: php codeigniter

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

这是我声明变量的地方。

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);


        }

所有这些都在同一个班级。

1 个答案:

答案 0 :(得分:3)

将此$this->$city更改为$this->city,其他所有内容都一样。不需要多余的$

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