在 codeigniter 中声明类级别变量
我是 CI 新手,我想做的是拥有一个类级别变量(例如是一个数组)。但尽管 CI 吹嘘得很厉害,但它似乎并不支持这个功能。用户指南中没有提及任何相关内容。有一个标题称为私有函数和变量,但文本似乎对变量保持沉默。
我想要这样的东西:
class OrderStats extends CI_Controller {
protected $arr_CoreCountry = ('0'=>'uk', '1'=>'us');
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function index()
{
$this->load->model('orders', '', TRUE);
//$data['result'] = $this->Testmodel->get_entries();
$data['result'] = $this->Testmodel->get_reports();
$this->load->view('test', $data);
}
public function getOrderStats()
{
$this->load->model('Orderstatsmodel', '', TRUE);
//$data['result'] = $this->Testmodel->get_entries();
foreach ($arr_CoreCountry as $key => $value)
{
$data['result'] = $this->Orderstatsmodel->get_orderStats($key);
}
// $data['result'] = $this->Orderstatsmodel->get_orderStats(0);
$this->load->view('orderstats', $data);
}
记住,当我在这篇文章中声明 $arr_CoreCountry 变量时,我不断地看到语法错误消息。 当我将它放置在任何函数内部的某个位置时,当然,它会超出范围,并且我不断收到一条错误消息,指出 $arr_CoreCountry 是一个未定义的变量。 那么问题是我在哪里定义它?
期待快速回复,因为仅仅因为 codeigniter 的这个垃圾,我就浪费了一半的时间。
I am new to CI and what I want to do is to have a class level variable (which e.g is an array). But it seems like CI, despite all high bragging, doesn't support this feature. Nothing has been mentioned in the user guide about it. There is a heading called private functions and variables but the text has been seemingly kept silent regarding variables.
I want to have something like :
class OrderStats extends CI_Controller {
protected $arr_CoreCountry = ('0'=>'uk', '1'=>'us');
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function index()
{
$this->load->model('orders', '', TRUE);
//$data['result'] = $this->Testmodel->get_entries();
$data['result'] = $this->Testmodel->get_reports();
$this->load->view('test', $data);
}
public function getOrderStats()
{
$this->load->model('Orderstatsmodel', '', TRUE);
//$data['result'] = $this->Testmodel->get_entries();
foreach ($arr_CoreCountry as $key => $value)
{
$data['result'] = $this->Orderstatsmodel->get_orderStats($key);
}
// $data['result'] = $this->Orderstatsmodel->get_orderStats(0);
$this->load->view('orderstats', $data);
}
Remember, when I declare $arr_CoreCountry variable at the place as it is in this post, I constantly see a syntax error message.
When I place it some where inside any function then of course, it gets out of scope and I keep getting an error messag that $arr_CoreCountry is an undefined variable.
So the question is where do I define it?
Expect a quick response as half of my day has been wasted just because of this s*** from codeigniter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该有效:
您在原始代码中省略了
$this->
。编辑
这是我的测试代码〜
也许您可以发布您的语法错误,因为它们似乎在您的原始帖子中丢失了。
This should work:
you are omitting the
$this->
in your original code.Edit
Here was my test code ~
perhaps you can post your syntax errors as they appear to be missing from your original post.
您有语法数组声明错误。请尝试像这样声明数组:
请查看此站点以获取数组手册:http: //php.net/manual/en/language.types.array.php
You have a syntax array declaration error. Please try to declare array like this:
Please check out this site for array manual: http://php.net/manual/en/language.types.array.php
我自己解决了这个问题。
我更改了两件事
protected $arr_CoreCountry = ('0'=>'uk', '1'=>'us');
更改为
foreach ($ arr_CoreCountry as $key => $value)
被更改为
我缺少 $this 但当我把它放在那里时,它仍然无法工作。当我将 protected 更改为 var 时,它起作用了。
感谢大家的意见...
I solved the problem myself.
There are two things which I changed
protected $arr_CoreCountry = ('0'=>'uk', '1'=>'us');
was changed to
and
foreach ($arr_CoreCountry as $key => $value)
was changed to
I was missing $this but when I put it there, it was still not working. When I changed protected to var, it worked.
Thanks everyone for your input...