Php 如何在类内调用全局对象函数,得到;未定义变量";错误

Php 如何在类内调用全局对象函数,得到;未定义变量";错误,php,predis,Php,Predis,我创建了这个简单的PHP,试图用Predis库在Redis上缓存请求,我在类外部实例化了$Redis对象,需要从类函数内部调用它。然而,我一直得到一个未定义的变量:redis message 我已经尝试从类范围内外调用缓存验证函数 代码如下: <?php require "predis/src/Autoloader.php"; Predis\Autoloader::register(); global $redis; $redis = new Predis\Client(); c

我创建了这个简单的PHP,试图用Predis库在Redis上缓存请求,我在类外部实例化了$Redis对象,需要从类函数内部调用它。然而,我一直得到一个未定义的变量:redis message

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

代码如下:

<?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');

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

您的
validateCache
函数在引用
$redis
时没有首先将其声明为全局变量,正如您在
SimpleJsonRequest
构造函数中所做的那样。

使用
global
停止。这是一种普遍的坏习惯,在课堂上是可怕的。只需输入您需要的内容:

class SimpleJsonRequest
{
    public function __construct($redis) {
        $this->redis = $redis;
    }
}

$redis = new Predis\Client();
$test = new SimpleJsonRequest($redis);
然后在需要时使用
$this->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
}

使用
全局
停止。这是一种普遍的坏习惯,在课堂上是可怕的。只需输入您需要的内容:

class SimpleJsonRequest
{
    public function __construct($redis) {
        $this->redis = $redis;
    }
}

$redis = new Predis\Client();
$test = new SimpleJsonRequest($redis);
然后在需要时使用
$this->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
}

你比我快,我几秒钟前就发现了这一点,并准备将其作为答案发布,但无论如何还是要感谢你的澄清。你比我快,我几秒钟前就发现了这一点,并准备将其作为答案发布,但无论如何都要感谢你的澄清。