php 控制器内的 Codeigniter 全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7151990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:09:40  来源:igfitidea点击:

Codeigniter global variable within controller

php

提问by ThomasReggi

I have a controller with a couple of methods and they all have the same variable within them. I am looking for a way to have them share the same variable. What is the best way to do this?

我有一个带有几个方法的控制器,它们都包含相同的变量。我正在寻找一种方法让它们共享相同的变量。做这个的最好方式是什么?

What I have now:

我现在所拥有的:

class Example extends CI_Controller{
  public function test{
    $variable = "awesome";
  }

  public function demo{
    $variable = "awesome";
  }

}

回答by 65Fbef05

How about...

怎么样...

class Example extends CI_Controller
{

  public $variable = "awesome";

  public function test()
  {
    echo $this->variable; // awesome
  }

  public function demo()
  {
    echo $this->variable; // awesome
  }

}