Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CodeIgniter-声明全局变量的最佳位置_Php_Codeigniter_Global Variables_Global - Fatal编程技术网

Php CodeIgniter-声明全局变量的最佳位置

Php CodeIgniter-声明全局变量的最佳位置,php,codeigniter,global-variables,global,Php,Codeigniter,Global Variables,Global,我只想在几个地方使用$variable:不仅在视图和控制器中,而且在routes.php和其他配置文件中 我不想要这样的东西:使用Config类加载配置文件;使用CI的获取实例,等等 我只想声明一个给定的$variable(它可以是一个常量,但我需要它作为一个变量),并在任何地方使用它 事实上。。。我想知道CI引导中的哪个PHP文件是最先被解析的文件之一,所以我可以在那里引入我的全局变量。。。但不是核心/系统或未经批准的文件,而是最适合此简单要求的“最佳”位置。在/application/con

我只想在几个地方使用
$variable
:不仅在视图和控制器中,而且在
routes.php
和其他配置文件中

我不想要这样的东西:使用Config类加载配置文件;使用CI的
获取实例
,等等

我只想声明一个给定的
$variable
(它可以是一个常量,但我需要它作为一个变量),并在任何地方使用它


事实上。。。我想知道CI引导中的哪个PHP文件是最先被解析的文件之一,所以我可以在那里引入我的全局变量。。。但不是核心/系统或未经批准的文件,而是最适合此简单要求的“最佳”位置。

/application/config
中有一个名为
constants.php的文件

我通常会把我所有的评论都放在那里,以便轻松看到它们的位置:

/**
 * Custom defines
 */
define('blah', 'hello mum!');
$myglobalvar = 'hey there';

加载
index.php
后,它将加载
/core/CodeIgniter.php
文件,然后依次加载公共函数文件
/core/common.php
,然后加载
/application/constants.php
,因此在这条链中,它是要加载的第四个文件。

您还可以创建一个constants\u helper.php文件,然后将变量放入其中。例如:

define('MY_CUSTOM_DIR', base_url().'custom_dir_folder/');
然后在应用程序/config/autoload.php上,自动加载contstants助手

$autoload['helper'] = array('contstants');

codeigniter
中声明
全局变量的最佳位置是
constants.php
目录下的
/application/config

您可以如下定义全局变量

/**
 * Custom definitions
 */
define('first_custom_variable', 'thisisit');
$yourglobalvariable = 'thisisimyglobalvariable';

内部文件application/conf/contants.php:

global $myVAR;
$myVAR= 'http://'.$_SERVER["HTTP_HOST"].'/';
并放入一些头文件或任何函数中:

global $myVAR;
$myVAR= 'some value';
我在助手文件中使用带有静态方法的“Globals”类来管理我的应用程序的所有全局变量。以下是我所拥有的:

globals_helper.php(在helpers目录中) 最后,为了在代码中的任何位置使用,您可以这样做来设置变量:

Globals::setAuthenticatedMemeberId('somememberid');
下面是阅读它的步骤:

Globals::authenticatedMemeberId()

注意:我将initialize调用留在Globals类中的原因是,如果需要的话,该类将来可以通过初始化器进行扩展。如果不需要对通过setter/getter设置和读取的内容进行任何控制,也可以将属性公开。

类似于上面的Spartak答案,但可能更简单

助手文件中的一个类,具有一个静态属性和两个读取和写入该静态属性的实例方法。无论创建多少实例,它们都会写入单个静态属性

在一个自定义帮助文件中创建一个类。还要创建一个返回该类实例的函数。该类定义了一个静态变量和两个实例方法,一个用于读取数据,一个用于写入数据。我这样做是因为我希望能够从控制器、模型、库、库模型收集日志数据,并收集所有日志数据,一次性发送到浏览器。我不想写入日志文件,也不想在会话中保存内容。我只是想收集在ajax调用期间服务器上发生的事情的数组,并将该数组返回到浏览器进行处理

在帮助文件中:

if (!class_exists('TTILogClass')){
    class TTILogClass{


        public static $logData = array();


        function TTILogClass(){


        }

        public function __call($method, $args){
            if (isset($this->$method)) {
                $func = $this->$method;
                return call_user_func_array($func, $args);
            }
        }

        //write to static $logData
        public function write($text=''){
            //check if $text is an array!
            if(is_array($text)){
                foreach($text as $item){
                    self::$logData[] = $item;
                }
            } else {
                self::$logData[] = $text;
            }
        }

        //read from static $logData
        public function read(){
            return self::$logData;
        }

    }// end class
} //end if

//an "entry" point for other code to get instance of the class above
if(! function_exists('TTILog')){
    function TTILog(){  
        return new TTILogClass();

    }
}
在任何控制器中,您可能希望输出由控制器或由控制器调用的库方法或由控制器调用的模型函数生成的所有日志项:

function testLogging(){
    $location = $this->file . __FUNCTION__;
    $message = 'Logging from ' . $location;

    //calling a helper function which returns 
    //instance of a class called "TTILogClass" in the helper file

    $TTILog = TTILog();

    //the instance method write() appends $message contents
    //to the TTILog class's static $logData array

    $TTILog->write($message);

    // Test model logging as well.The model function has the same two lines above,
    //to create an instance of the helper class TTILog
    //and write some data to its static $logData array


    $this->Tests_model->testLogging();

    //Same thing with a library method call
    $this->customLibrary->testLogging();

    //now output our log data array. Amazing! It all prints out!
    print_r($TTILog->read());
}
打印出:

从controllerName记录:testLogging

从modelName进行日志记录:testLogging


来自customLibrary的日志记录:testLogging

,尽管如果我们在
constants.PHP
中声明一个全局变量,PHP文件名会产生误导。。。这似乎是个好地方。我已经添加了我的
$GLOBALS['variable']='mystuff'那里的代码,它可以在
routes.php
上找到,正如我所希望的,所以它肯定可以在控制器、视图等处找到+1(我将等待其他答案,然后再接受作为解决方案;也许有人有更好的建议。)CodeIgniter非常灵活,例如,您可以完全调整
index.php
文件,并在其中填充变量(如果您愿意的话):)我只是喜欢将我的所有内容都保留在常量文件中(至少在我看来)或者更好的建议是在
index.php
文件顶部的另一个文件中包含自定义变量。可能更合你的意。的确如此。:)并不是说我是一个全球变量迷。。。这将是近10年来我第一次使用
$GLOBALS
!它非常适合快速、有效、简单地解决我的实际问题。您的新建议很好。现在,我将继续使用
constants.php
,因为它是最简单的解决方案,并且它位于
应用程序
文件夹中的一个标准文件中。。。在我看来,与修改CI的
index.php
相比,似乎没有那么“黑客”。非常感谢。每次返回空值时,它都不工作。
if (!class_exists('TTILogClass')){
    class TTILogClass{


        public static $logData = array();


        function TTILogClass(){


        }

        public function __call($method, $args){
            if (isset($this->$method)) {
                $func = $this->$method;
                return call_user_func_array($func, $args);
            }
        }

        //write to static $logData
        public function write($text=''){
            //check if $text is an array!
            if(is_array($text)){
                foreach($text as $item){
                    self::$logData[] = $item;
                }
            } else {
                self::$logData[] = $text;
            }
        }

        //read from static $logData
        public function read(){
            return self::$logData;
        }

    }// end class
} //end if

//an "entry" point for other code to get instance of the class above
if(! function_exists('TTILog')){
    function TTILog(){  
        return new TTILogClass();

    }
}
function testLogging(){
    $location = $this->file . __FUNCTION__;
    $message = 'Logging from ' . $location;

    //calling a helper function which returns 
    //instance of a class called "TTILogClass" in the helper file

    $TTILog = TTILog();

    //the instance method write() appends $message contents
    //to the TTILog class's static $logData array

    $TTILog->write($message);

    // Test model logging as well.The model function has the same two lines above,
    //to create an instance of the helper class TTILog
    //and write some data to its static $logData array


    $this->Tests_model->testLogging();

    //Same thing with a library method call
    $this->customLibrary->testLogging();

    //now output our log data array. Amazing! It all prints out!
    print_r($TTILog->read());
}