我在 Controller 中定义了静态变量,但是当我在函数中使用该变量时,它给出了 undefined variable 错误。
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Quiz extends Admin_Controller {
private static $secure_key = "aXXXXXXXXc";
public function __construct()
{
parent::__construct();
}
public function edit($id)
{
try
{
$token = JWT::encode($postdata, $secure_key);
echo "<pre>";print_r($token);exit;
}
catch(Exception $e){
$this->data['error'] = $e->getMessage();
redirect('/','refresh');
}
}
}
$token
使用 jwt 正确打印,但出现错误
Undefined variable: secure_key
我尝试了不同的方法将 $secure_key
定义为
public static $secure_key = "aXXXXXXXc;
static $secure_key = "aXXXXXXXc;
我尝试在构造函数中将 $secure_key
定义为
$secure_key = "aXXXXXXXc;
但是没有用。为什么这样?请帮忙。我正在使用 codeigniter 3
最佳答案
推荐方法(基于安全性)
在config.php
中定义变量并访问它。这将像全局变量一样工作
$config['secure_key'] = 'myKey';
$this->config->item('secure_key'); # get
$this->config->set_item('secure_key', 'NewKey'); # set
<小时/>
像这样访问它
$this->$secure_key
self::$secure_key
如果函数
$this->function_name();
关于php - 函数中调用的静态变量给出错误未定义的 codeigniter php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46807136/