php - 函数中调用的静态变量给出错误未定义的 codeigniter php

标签 php codeigniter codeigniter-3 static-variables

我在 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

根据 Comment by cd001

self::$secure_key 

如果函数

$this->function_name();

关于php - 函数中调用的静态变量给出错误未定义的 codeigniter php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46807136/

相关文章:

javascript - 如何将动态输入字段提交到数据库中?

php - 如何获取join中每个bank_id的最后记录

javascript - 使用我的 PHP 网站记录谁从 S3 下载了哪些文件

php - 什么是绿色版的php?

php - 用户使用phonegap应用程序登录并保持登录状态?

mysql - 如何在 where 子句中使用 SOUNDS LIKE

php - 如果为空,Laravel 密码验证在 "min length"上失败

php - 如何在网站上嵌入谷歌财经历史图表?

Codeigniter:显示RAW查询结果

php - SQL 连接两个表?选择名称类别 - CodeiGniter3