简体   繁体   English

CodeIgniter无法访问控制器中的全局变量

[英]CodeIgniter can not access global variable in controller

I have already tried lots of ways. 我已经尝试了很多方法。 add library, add config file, add a controller, just add in same controller.........etc. 添加库,添加配置文件,添加控制器,只需添加相同的控制器.........等等。 This also have same problem: (this is add in same controller) 这也有同样的问题:(这是添加在同一控制器中)

<?php
class Test extends CI_Controller{
  public $data = array();

  public function __construct(){
    parent::__construct();
    //if call add_data() here, it is work
  }

  function add_data(){
    $arraya = array('a'=>'aa', 'b'=>'bb');
    $this->data = $arraya;
  }

  function index(){
    $this->add_data();
  }

  function want_print(){
    print_r($this->data);
  }
}
?>

if I call add_data in index, i cannot get any data in want_print().... if I call add_data in the construct, i can get data in want_print().. 如果在索引中调用add_data,则无法在want_print()中获取任何数据。...如果在结构中调用add_data,则可以在want_print()中获取数据。

Please anyone help me solve this problem? 请有人帮我解决这个问题吗? I don't want to call it in construct because i will not call it every time... 我不想在结构中调用它,因为我不会每次都调用它...

You can set the data in your want_print() function like this: 您可以像这样在want_print()函数中设置数据:

function want_print() {
    $this->index();
    print_r($this->data);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM