Home >Backend Development >PHP Tutorial >It's very strange. Phar introduced phar, but an error was reported, saying that the PredisClient class could not be found?
<code><?php require 'Predis.phar'; use Predis\Client; ini_set('session.save_path', 'tcp://localhost:6379'); ini_set('session.name', 'YMFSESSION'); ini_set('session.save_handler', 'user'); class MySession implements SessionHandlerInterface { private $redis; private function connect() { if(!$this->redis) { $cfg = [ 'scheme' => 'tcp', 'host' => '127.0.0.1', 'port' => 6379 ]; $this->redis = new \Predis\Client($cfg); } } /** * Close the session * @since 5.4.0 */ public function close() { $this->redis->quit(); return true; } /** * Destroy a session * @since 5.4.0 */ public function destroy($session_id) { $this->connect(); return $this->redis->del($session_id); } /** * Cleanup old sessions * @since 5.4.0 */ public function gc($maxlifetime) { return true; } /** * Initialize session * @since 5.4.0 */ public function open($save_path, $session_id) { return true; } /** * Read session data * @since 5.4.0 */ public function read($session_id) { $this->connect(); $data = $this->redis->get($session_id); return $data; } /** * Write session data * @since 5.4.0 */ public function write($session_id, $session_data) { $this->connect(); $expire = configure('Ymf.Account.expire'); if(is_int($expire) && $expire > 0) { $result = $this->redis->setex($session_id, $expire, $session_data); $re = $result ? 'true' : 'false'; }else{ $result = $this->redis->set($session_id, $session_data); $re = $result ? 'true' : 'false'; } var_dump($result); return $re; } } session_set_save_handler(new MySession()); session_start(); $_SESSION['name'] = 43; </code>
This is the directory
<code><?php require 'Predis.phar'; use Predis\Client; ini_set('session.save_path', 'tcp://localhost:6379'); ini_set('session.name', 'YMFSESSION'); ini_set('session.save_handler', 'user'); class MySession implements SessionHandlerInterface { private $redis; private function connect() { if(!$this->redis) { $cfg = [ 'scheme' => 'tcp', 'host' => '127.0.0.1', 'port' => 6379 ]; $this->redis = new \Predis\Client($cfg); } } /** * Close the session * @since 5.4.0 */ public function close() { $this->redis->quit(); return true; } /** * Destroy a session * @since 5.4.0 */ public function destroy($session_id) { $this->connect(); return $this->redis->del($session_id); } /** * Cleanup old sessions * @since 5.4.0 */ public function gc($maxlifetime) { return true; } /** * Initialize session * @since 5.4.0 */ public function open($save_path, $session_id) { return true; } /** * Read session data * @since 5.4.0 */ public function read($session_id) { $this->connect(); $data = $this->redis->get($session_id); return $data; } /** * Write session data * @since 5.4.0 */ public function write($session_id, $session_data) { $this->connect(); $expire = configure('Ymf.Account.expire'); if(is_int($expire) && $expire > 0) { $result = $this->redis->setex($session_id, $expire, $session_data); $re = $result ? 'true' : 'false'; }else{ $result = $this->redis->set($session_id, $session_data); $re = $result ? 'true' : 'false'; } var_dump($result); return $re; } } session_set_save_handler(new MySession()); session_start(); $_SESSION['name'] = 43; </code>
This is the directory
use means using namespace
use PredisClient;
Use the client class in the Predis space
<code>use \Predis\Client;</code>
Or since the complete namespace is used below, just remove use directly