首頁 > 後端開發 > php教程 > 很奇怪,php 引進了phar,但報錯,說PredisClient類別找不到?

很奇怪,php 引進了phar,但報錯,說PredisClient類別找不到?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
發布: 2016-09-09 08:28:00
原創
1126 人瀏覽過

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

<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>

登入後複製
登入後複製

這是目錄
很奇怪,php 引進了phar,但報錯,說PredisClient類別找不到?

回覆內容:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

<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>

登入後複製
登入後複製

這是目錄
很奇怪,php 引進了phar,但報錯,說PredisClient類別找不到?

use 是使用命名空間的意思

use PredisClient;

使用Predis空間下的 client 類別

1

<code>use \Predis\Client;</code>

登入後複製

或即然下面使用時用的完整命名空間,直接把use去掉

相關標籤:
php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
怎麼學好php
來自於 1970-01-01 08:00:00
0
0
0
PHP擴充intl
來自於 1970-01-01 08:00:00
0
0
0
php數據獲取?
來自於 1970-01-01 08:00:00
0
0
0
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板