4

Answer: I had to change the path of PREDIS_BASE_PATH to predis/lib/.

I want to load predis inside of a PHP file, but I am having trouble. I am following the guide to load predis on the predis github website (https://github.com/nrk/predis). Here is the code that I am using to load predis:

define("PREDIS_BASE_PATH", "predis/");
echo "The predis base path is: " . PREDIS_BASE_PATH . "\n";
spl_autoload_register(function($class) {
  $file = PREDIS_BASE_PATH . strtr($class, '\\', '/') . '.php';
  echo "The file variable is: " . $file . "\n";
  if (file_exists($file)) {
    require $file;
    return true;
  }
});

$redis = new Predis\Client(array(
  'host'  => 'localhost',
  'port'  => 6379,
));

Here is the error that I get:

Fatal error: Class 'Predis\Client' not found

Edit: What file in the predis directory should be imported? After changing the folder permissions, I am able to echo what the variable $file is holding: "The file variable is: predis/Predis/Client.php"

According to the directory listing here, https://github.com/nrk/predis, there is no client.php file.

j0k
  • 22,600
  • 28
  • 79
  • 90
egidra
  • 347
  • 2
  • 9
  • 18
  • as the error says, the class is not in the current scope. sure $file is correct? –  Jun 28 '11 at 23:10

5 Answers5

13

I use below code to connect predis on php page, and it worked fine.. below is code

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

        $redis = new Predis\Client(array(
         "scheme" => "tcp",
         "host" => "127.0.0.1",
         "port" => 6379));
?>
Suhel Meman
  • 3,702
  • 1
  • 18
  • 26
3

write the below code to call the register method:

Predis\Autoloader::register();

instead of PredisAutoloader::register();

And put your test file parallel to Predis Folder.

Aman Garg
  • 3,122
  • 4
  • 24
  • 32
1

I used composer to install Redis and got a but of hard time making it work. Eventually the following script worked.

define('__ROOT__', dirname(dirname(__FILE__))); 
echo "Root ".dirname(dirname(__FILE__));
require_once(__ROOT__.'/vendor/autoload.php');//load all PHP dependencies
//require_once(__ROOT__.'/vendor/predis/predis/autoload.php'); //load only Redis also works
$REDIS_SERVER="127.0.0.1";
$REDIS_PORT=6379;
try 
{
    echo "<p>Connecting to Redis $REDIS_SERVER:$REDIS_PORT";

    $redis = new Predis\Client(array(
        "scheme" => "tcp",
        "host" => $REDIS_SERVER,
        "port" => $REDIS_PORT
    ));
    echo "<p>Hello I am Redis";
...
Stan Sokolov
  • 2,140
  • 1
  • 22
  • 23
1
$redis = new Predis\Client(array(

should be

$redis = new Predis_Client(array(
Colum
  • 3,844
  • 1
  • 22
  • 26
  • 1
    Yes, but that does not work. If you look in the code of Predis.php, there is no class Predis\Client, but there is Predis_Client. It is an error that has gotten me as well. – Colum Jun 29 '11 at 00:43
0

Your code looks perfectly fine. I can only assume that you aren't importing the class correctly. Are you sure $file is where the code is assuming it is.

Double check it's there and set the permissions to 777 using sudo chmod -R 777 /path/to/file and see if that works.

Reset the permissions to something more secure afterwards if it works/doesn't work.

Hope this helps

EDIT:

Download Predis.php, put it in the same directory as the file with your php code in it, and make the code look like this:

  spl_autoload_register(function($class) {
  $file =  strtr($class, '\\', '/') . '.php';
  echo "The file variable is: " . $file . "\n";
  if (file_exists($file)) {
    require $file;
    return true;
  }
});

$redis = new Predis\Client(array(
  'host'  => 'localhost',
  'port'  => 6379,
));