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.

1
  • as the error says, the class is not in the current scope. sure $file is correct? Commented Jun 28, 2011 at 23:10

5 Answers 5

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));
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Why there is instance twice, $redis = new Predis\Client(); $redis = new Predis\Client(array(...
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.

Comments

1
$redis = new Predis\Client(array(

should be

$redis = new Predis_Client(array(

1 Comment

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.
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";
...

Comments

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,
));

3 Comments

Which file should be imported?
Predis.php is the file required
Where can the predis.php file be located? I am looking in the predis/lib directory, but I cannot find it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.