CodeIgniter Forums
Class variables? - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: Class variables? (/thread-48118.html)

Class variables? - El Forum - 01-05-2012

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:

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->form_validation->set_rule('number', 'Number', 'callback_check_nr');
        if($this->form_validation->run() == FALSE) {
        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!