Php 如何使用class函数和init文件获取redis键值

Php 如何使用class函数和init文件获取redis键值,php,redis,Php,Redis,在我的项目中,我使用redis 我有一个init文件,包括ip端口和端口,所以类Datasource用于分析init文件和连接redis 下面是包含函数getRedis()的Datasource.php类代码: 以下是redis.ini.php的init文件代码 <?php $config['redis']['instance1'] = array( 'default' => array( 'host' => '127.0.0.1', 'port' =>

在我的项目中,我使用redis

我有一个init文件,包括ip端口和端口,所以类Datasource用于分析init文件和连接redis

下面是包含函数getRedis()的Datasource.php类代码:

以下是redis.ini.php的init文件代码

<?php
 $config['redis']['instance1'] = array(
'default' => array(
    'host' => '127.0.0.1',
    'port' => '6379',
    'timeout' => 5,
    'pconnect' => 1,
    'password' => '',
  )
);
 $config['redis']['instance2'] = array(
'default' => array(
    'host' => '127.0.0.1',
    'port' => '6379',
    'timeout' => 5,
    'pconnect' => 1,
    'password' => '',
    )
  );

其实谢键应该是左。正确的结果是一句话:“得键谢是:左”


但是它没有显示任何内容,谁能帮助我呢?

您在静态方法中使用
$this
,而您不能。此外,您在连接到Redis时捕获所有异常,因此您不知道为什么它没有失败。你需要做两件事:

  • 打开PHP错误(当然,仅用于开发)
  • 连接时不要捕获异常,如果捕获,则记录/保留异常消息
  • 试着这样做:

    <?php
    
    namespace common;
    
    class Datasource
    {
        private static $_redis, $_config_name, $_redis_config, $_server_region;
    
        public static function getRedis($config_name = NULL, $server_region = 'default')
        {
            error_reporting(E_ALL);
            ini_set("display_errors", true);
    
            global $config;
    
            $redis_config = $config['redis'][$config_name];
    
            if (!$config_name || !$redis_config || !$server_region) {
                throw new \Exception('$config_name or $redis_config or $server_region is not set');
            }
    
            if (!$redis_config[$server_region]['password']) {
                throw new \Exception('Redis password is not set');
            }
    
            self::$_config_name = $config_name;
            self::$_redis_config = $redis_config;
            self::$_server_region = $server_region;
    
            self::$_redis = new \Redis();
    
            self::$_redis->connect(self::$_redis_config[$server_region]['host'], self::$_redis_config[$server_region]['port']);
    
            if (!self::$_redis->auth(self::$_redis_config[$server_region]['password'])) {
                throw new \Exception("Can't login to Redis. Check password");
            }
    
            return self::$_redis;
        }
    }
    
    public static function getRedis(...)
    {
        if (!self::$_redis) {
            ...
        }
    
        return self::$_redis;
    }
    

    其中
    xie
    值设置为
    zuo
    ?在我的redis服务器中,我定义了xie值。在客户端行中,我可以成功获取xie并获取ZO返回值do
    var\u dump($redis\u obj)
    。它是空的吗?@htmlhell,我用了“echo var_dump($redis_obj);”,它什么都不是没有echo。只是
    var\u转储($redis\u obj)紧跟在
    $redis\u obj=common\Datas…
    <?php
    
    namespace common;
    
    class Datasource
    {
        private static $_redis, $_config_name, $_redis_config, $_server_region;
    
        public static function getRedis($config_name = NULL, $server_region = 'default')
        {
            error_reporting(E_ALL);
            ini_set("display_errors", true);
    
            global $config;
    
            $redis_config = $config['redis'][$config_name];
    
            if (!$config_name || !$redis_config || !$server_region) {
                throw new \Exception('$config_name or $redis_config or $server_region is not set');
            }
    
            if (!$redis_config[$server_region]['password']) {
                throw new \Exception('Redis password is not set');
            }
    
            self::$_config_name = $config_name;
            self::$_redis_config = $redis_config;
            self::$_server_region = $server_region;
    
            self::$_redis = new \Redis();
    
            self::$_redis->connect(self::$_redis_config[$server_region]['host'], self::$_redis_config[$server_region]['port']);
    
            if (!self::$_redis->auth(self::$_redis_config[$server_region]['password'])) {
                throw new \Exception("Can't login to Redis. Check password");
            }
    
            return self::$_redis;
        }
    }
    
    public static function getRedis(...)
    {
        if (!self::$_redis) {
            ...
        }
    
        return self::$_redis;
    }