Codeigniter类变量正在重置 [英] Codeigniter class variables are resetting

查看:96
本文介绍了Codeigniter类变量正在重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道发生了什么,但是非常令人沮丧.我的代码:

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

问题是我可以更改此变量的值.但是当我导航到另一个页面时,此变量将重置为其原始值:1.为什么?

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?

似乎模型每次都在加载?所以变量总是定义为1?

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

我尝试不分配任何值,然后在获取变量时返回null.

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

我不知道该怎么做

推荐答案

PHP本质上是无状态的.这意味着,对于每个请求,将创建您所使用的所有类的新实例,并按照代码的class {}位中的定义对其进行初始化.由于您的类声明并像这样初始化属性:

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;

您说您尝试 not 分配一个值,这导致吸气剂重现null.同样,这是可以预期的:默认情况下,未初始化的PHP变量实际上被初始化为null.就像JavaScript变量被初始化为undefined.

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.

无论如何,所有这些都意味着每个实例都会在tree_selected_item设置为1的情况下开始使用.如果要跟踪两次请求之间的值,则必须使用$_POST$_GET$_COOKIE或最好是$_SESSION来存储该数据.然后,您只需在类中定义一个构造函数,该构造函数将所需的值用作参数,即:

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

看到您已经为这个问题标记了 codeigniter ,您可能希望更改编码样式,以符合

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

因此,每当需要一个tree_selected_item值来在两次请求之间保留时,您就必须执行以下操作(我没有使用CI,所以我不得不把它留给您来进行转换到CI处事方式的代码):

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

当然,您会发现,随着应用程序的增长,要存储的值的绝对数量变得相当大.到那时,您将开始使用会话来识别用户,并使用数据库来存储所有相关数据.这就是为什么PHP传统上与各种DB串联使用的原因(即LA(N)MP堆栈-Linux(服务器操作系统)Apache(网络服务器)(否)MySQL和PHP,应用程序逻辑)

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)

这篇关于Codeigniter类变量正在重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆