Codeigniter class variables are resetting

2019-08-06 12:44发布

问题:

I don't know what is happening but it's very frustrating. My code:

 class Catalog extends CI_Model {

     private $tree_selected_item = 1;

     public function get_tree_item_selected() {
         return empty($this->tree_selected_item) ? 1 : $this-> 

     public function set_tree_item_selected($i){
         $this->tree_selected_item = $i;
}

So I can use that wherever I want doing:

 $this->catalog->set_tree_item_selected($i);
 $selected = $this->catalog->get_tree_item_selected();

The problem is I can change the value of this variable. but when I navigate to another page, this variable resets to its originally value: 1. Why?

It seems that the model is loading everytime? so the variable always is defining to 1?

I tried without assigning a value, and then when I get the variable returns null.

I don't know what to try

回答1:

PHP is, essentially, stateless. That means that, for every request, a new instance of all of the classes you use will be created, and initialized as defined in the class {} bit of code. Since your class declares, and initializes the property like so:

private $tree_selected_item = 1;

You say you've tried not assigning a value, which resulted in the getter returing null. This, again, is to be expected: PHP variables that are not initialized are in fact initialized to null by default. Just like JavaScript variables are initialized to undefined.

Anyway, all of this means that every instance will start out in life with a tree_selected_item set to 1. If you want to keep track of a value in between requests, you'll have to use $_POST, $_GET, $_COOKIE or preferably $_SESSION to store that data. Then, you simply define a constructor in your class that takes the values you want as arguments, ie:

public function __constructor($treeItem = 1)
{
    $this->tree_selected_item = (int) $treeItem;
}

Seeing as you've tagged this question codeigniter, you might want to change your coding style, to conform to the codeigniter standards. Meaning the allman-style indentation (opening {'s go on a separate line and all that).

So whenever you need a value of tree_selected_item to persist in between requests, you'll have to do something like this (I haven't used CI, so I'll have to leave it up to you to transform this code to the CI-way of doing things):

//session_start(); should be called prior to this point
$_SESSION['treeItem'] = $instance->get_tree_selected_item();

So come the next request, you can initialize the instance correctly:

//again, you need a session_start call
$instance = new Catalog($_SESSION['treeItem']);

Of course you'll find that, as your application grows, the sheer number of values you want to store gets rather large. It's at that point that you'll start using sessions as a means to identify users, and use a DB to store all the relevant data. That's why PHP is traditionally used in tandem with a DB of sorts (ie the LA(N)MP stack - Linux (Server OS) Apache (Webserver) (No)MySQL and PHP, the application logic)