【问题标题】:How to call global object function inside class, getting "Undefined variable" error如何在类中调用全局对象函数,出现“未定义变量”错误
【发布时间】:2019-02-11 22:21:02
【问题描述】:

我正在创建这个简单的 PHP,试图使用 Predis 库在 Redis 上缓存请求,我在类外部实例化了我的 $redis 对象,我需要从类函数内部调用它。但是我不断收到未定义的变量:redis 消息。

我已经尝试在类范围内外调用我的缓存验证函数。

代码如下:

<?php
require "predis/src/Autoloader.php";
Predis\Autoloader::register();

 global $redis;

 $redis = new Predis\Client();

class SimpleJsonRequest
{
    private $redisLocal;

    public function __construct() {
        global $redis;
        $this->redisLocal =& $redis;
    }

    private static function makeRequest(string $method, string $url, array $parameters = null, array $data = null)
    {
        $opts = [
            'http' => [
                'method'  => $method,
                'header'  => 'Content-type: application/json',
                'content' => $data ? json_encode($data) : null
            ]
        ];
        $url .= ($parameters ? '?' . http_build_query($parameters) : '');
        return file_get_contents($url, false, stream_context_create($opts));
    }
    public static function get(string $url, array $parameters = null)
    {
        if(validateCache('GET', $url, $parameters))
            return json_decode(self::makeRequest('GET', $url, $parameters));
    }
    public static function post(string $url, array $parameters = null, array $data)
    {
        if(validateCache('POST', $url, $parameters, $data))
            return json_decode(self::makeRequest('POST', $url, $parameters, $data));
    }
    public static function put(string $url, array $parameters = null, array $data)
    {
        if(validateCache('PUT', $url, $parameters, $data))
            return json_decode(self::makeRequest('PUT', $url, $parameters, $data));
    }   
    public static function patch(string $url, array $parameters = null, array $data)
    {
        if(validateCache('PATCH', $url, $parameters, $data))
            return json_decode(self::makeRequest('PATCH', $url, $parameters, $data));
    }
    public static function delete(string $url, array $parameters = null, array $data = null)
    {
        if(validateCache('DELETE', $url, $parameters, $data))
            return json_decode(self::makeRequest('DELETE', $url, $parameters, $data));
    }
}

function validateCache(string $method, string $url, array $parameters = null, array $data = null)
{
    if($redis->exists($method)
        && $redis->get($method, 'url') == $url 
        && json_decode($redis->get($method, 'parameters')) == $parameters
        && json_decode($redis->get($method, 'data')) == $data)
    {
        echo'request cached';
        return false;
    }
    else
    {
        $redis->hMset($method, [
                        'url' => $url,
                        'parameters' => $parameters = null ? null : json_encode($parameters),
                        'data' => $data = null ? null : json_encode($data)
                    ]);

        $redis->expire($method, 10);

        echo 'not cached';
        return true;
    }
}

$test = new SimpleJsonRequest();
print_r($redis);
echo $test->get('1');
echo $test->get('1');

我希望它首先打印“未缓存”,然后是“请求缓存”,但我不断收到未定义变量错误。

【问题讨论】:

    标签: php predis


    【解决方案1】:

    您的 validateCache 函数引用 $redis 而不首先将其声明为全局变量,就像您在 SimpleJsonRequest 构造函数中所做的那样。

    【讨论】:

    • 你比我快,我在几秒钟前就发现了,正要发布它作为答案,但无论如何感谢您的澄清。
    【解决方案2】:

    停止使用global。一般来说,这是不好的做法,而且在课堂上很可怕。只需传递您需要的内容:

    class SimpleJsonRequest
    {
        public function __construct($redis) {
            $this->redis = $redis;
        }
    }
    
    $redis = new Predis\Client();
    $test = new SimpleJsonRequest($redis);
    

    然后在任何需要的地方使用$this-&gt;redis,例如validateCache

    if($this->redis->exists($method)
        && $this->redis->get($method, 'url') == $url 
        && json_decode($this->redis->get($method, 'parameters')) == $parameters
        && json_decode($this->redis->get($method, 'data')) == $data)
    {
        // rest of code
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-22
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多