Code igniter how to access a public variable inside a class.

I have a problem accessing a Public declared variable in the methods inside a class of a controller in codeigniter.

this is where I declared the variables .

Class Main_controller extends CI_Controller{




        public $firstname = "";
        public $middlename = "";
        public $lastname = "";
        public $suffix = "";
        public $age = "";
        public $city = "";
        public $street = "";
        public $house = "";

And I would like to acess it from this method , but It's telling me that the variables aren't declared.

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


        }

all of these are in the same class.

Upvotes: 1

Views: 37

Answers (1)

Alex
Alex

Reputation: 9265

Change this $this->$city to $this->city, same goes for all the others. The extra $ isn't necessary.

I might add that the way you are using class variables is odd as it won't persist when the page is changed/refreshed.

Upvotes: 3

Related Questions