CodeIgniter:通过控制器的全局变量和对重载属性的间接修改 [英] CodeIgniter: Global Vars via Controller and Indirect modification of overloaded property

查看:122
本文介绍了CodeIgniter:通过控制器的全局变量和对重载属性的间接修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自CodeIgniter成立之初到现在,我一直困扰着我一个问题,对于新的CI 3,我想看看是否有更优雅的解决方法。

A problem haunting me since early days of CodeIgniter and now, with the new CI 3 i want to see if there is a more elegant way to solve it.

// file: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {

   public $GLO;

   function __construct(){
      parent::__construct();
      $this->GLO['foo'] = 'bar';
      $this->GLO['arr'] = array();
   }
}

然后,在代码后面,我需要获取并动态设置$ GLO变量的值。因此,例如:

then, later in the code, I need to get and set the values of the $GLO variable dynamically. So for instance:

// file: application/controllers/dispatcher.php
class Dispatcher extends MY_Controller {

   function __construct() {
     parent::__construct();
     $this->load->model('public/langs');
     print_r($this->GLO);
   }
}

将打印 array(' foo'=>'bar,'arr'=> Array())是正确的。同样在我的模型中,我可以以相同的方式获取$ GLO数组的值。但是,一旦我需要在$ GLO数组中设置任何值,就会得到重载属性通知的间接修改,因此陷入困境。在我的模型中(执行数据库查询后):

will print array('foo'=>'bar, 'arr'=>Array()) which is correct. Also in my models I can get the values of the $GLO array in the same manner. However, as soon as I need to set any values in the $GLO array, I get the Indirect modification of overloaded property notice and so I am stuck. In my model (after executing a DB query):

// file: application/models/public/langs.php
class Langs extends CI_Model {

function __construct(){
    parent::__construct();  
}

function set_global_languages(){
  print_r($this->GLO); // <<< prints the same values as in the controller above
  $temp = array();
  // [stripped db code]
  $temp['label'] = $row->label;
  $temp['id'] = $row->id;      
  $this->GLO['arr'][] = $temp; // <<< this is where the notice happens
}

关于如何使用<$ c的任何线索$ c> $ this-> GLO ['foo'] ='baz'; 用于在我的模型中设置此全局数组的属性?

Any clues of how I can use $this->GLO['foo'] = 'baz'; for setting properties of this global array in my models?

干杯。

推荐答案

sintakonte ,非常感谢。昨晚我不得不重写大量代码以查看其是否起作用以及如何起作用。 Registry类按预期工作!这种set / get方法的唯一问题是对深层数组的处理,尤其是那些动态创建的数组以及它们的数据也被动态推送的数组。所以今天早上我做了一件非常糟糕的事情:),并创建了这个类:

sintakonte, thanks a lot for the tip. Last night I had to rewrite a lot of code to see if and how it works. The Registry class works as expected! The only issue with this set/get method is the handling of deep arrays, especially those created dynamically and having their data pushed dynamically too. So today morning I did something very bad :) and just created this class:

class Globals {
  public $data = array();
}

所以现在我要从 $ this-> ; GLO ['foo'] $ this-> globals-> data ['foo'] 并保持现有语法完整!这也使我可以在使用不同名称的不同控制器模型中重用该类,一旦将其初始化为 globals,另一次初始化为 admin。似乎也可以正常工作!

So now I am going from $this->GLO['foo'] to $this->globals->data['foo'] and get to keep existing syntax intact! It also enables me to reuse the class in different controllers and models under different names, once initializing it as "globals" and the other time as "admin". Seems to work alright, too!

这篇关于CodeIgniter:通过控制器的全局变量和对重载属性的间接修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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