• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class variables?

#1
[eluser]zxcv[/eluser]
I'm trying to maintain access to certain values within a class. What I do is that I declare those variables as class ones. With this concept I expect to be able to reach these values from every function within the class. But I'm starting to believe that this is the wrong approach, due to the variable being unset as soon as a certain function is left.

The "concept" of my code is similiar to this:

Code:
class Something extends CI_Controller {

    var $old_number;    //this is the number I want to keep track of

    public function Something() {
        //...
    }

    public function index($param = 10) {
        $this->old_number = $param;    //the old number has the value 10 here
        $this->load->view('some_form1');    //a formview with action set to the function below
    }

    public function the_form_validator() {
        $this->load->library('form_validation');
        $this->form_validation->set_rule('number', 'Number', 'callback_check_nr');
        
        if($this->form_validation->run() == FALSE) {
            $this->load->view('some_form2');
        }
        else {
            //...
        }
    }

    public function check_nr($nr = 10) {
        if($nr == $this->old_number) {
            echo "They are equal!";
        }
        else {
            echo "They are not equal!";
        }
        
        echo "Old number: " . $this->old_number;
        echo "New number: " . $nr;
    }
}

The output is:
Quote:They are not equal!
Old number: 0
New number: 10


What am I doing wrong?
My intention is to have the old number remained as 10. It seems to be unset as soon as the index function has done its job!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.