Search code examples
phppredis

How to call global object function inside class, getting "Undefined variable" error


I'm creating this simple PHP trying to cache requests on Redis with the Predis library, I instantiated my $redis object outside of the class and I need to call it from inside the class functions. However I keep getting the Undefined variable: redis message.

I have already tried calling my cache validating function both from inside and outside the class scope.

Here is the code:

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

I expected it to print "not cached" first followed by "request cached" but i keep getting Undefined variable error.


Solution

  • Your validateCache function is referencing $redis without first declaring it as a global variable, as you did in your SimpleJsonRequest constructor.