在函数中调用的静态变量给出错误 undefined codeigniter php
static variable called in function giving error undefined codeigniter php
我已经在控制器中定义了静态变量,但是当我在函数中使用该变量时,它给出了未定义变量错误。
控制器
<?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();
因为 $secure_key
在您的 class 中声明为 static
。因此可以使用 self
或 className
作为
来访问它
self::$secure_key
或
Quiz::$secure_key
我已经在控制器中定义了静态变量,但是当我在函数中使用该变量时,它给出了未定义变量错误。
控制器
<?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();
因为 $secure_key
在您的 class 中声明为 static
。因此可以使用 self
或 className
作为
self::$secure_key
或
Quiz::$secure_key