Home  >  Article  >  Backend Development  >  Introduction to the method of modifying Cache source code in ThinkPHP 5.1 (code example)

Introduction to the method of modifying Cache source code in ThinkPHP 5.1 (code example)

不言
不言forward
2019-02-11 09:52:432321browse

This article brings you an introduction to the method of modifying the Cache source code of ThinkPHP 5.1 (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

I’ve been learning THinkPHP 5.1 recently, and after seeing the operation of the Cache method, I’m a little confused. Although it encapsulates many methods and is very convenient to use, it does not seem to be very friendly to advanced operations of Redis. For the purpose of learning, the source code has been slightly modified. First, let me make two points: first, this modification is just a personal opinion and not suitable for everyone; second, this modification is only for learning, please be careful when modifying the source code.

Question

When I was practicing Redis, I found that if you want to use advanced methods, such as hSet, hGet, etc., you must first return the handle before executing it. As you can see below

<?php
namespace app\index\controller;
use think\cache\driver\Redis;
use think\Controller;
class RedisTest extends Controller
{
    public function index()
    {
        $redis = new Redis();
        $redis = $redis->handler();

        dump($redis->hSet('h_name', '1', 'tom'));// int(1)
    }
}

, the execution was successful. The question is why the handle should be returned first, which can be solved with the magic method of __call.

Tracking source code

Since you have doubts, you must clear them up. Tracing the source code, I saw thinkphp/library/think/cache/Driver.php and found that there was indeed no __call, just handler to return the handle to execute advanced methods. I don't understand why __clss is not used.

Solution to the problem

The solution is to add the __call method in thinkphp/library/think/cache/Driver.php, so that not only Redis can use advanced methods directly, but also other Cache classes that inherit this file can be used directly. The code is as follows

     /**
     * 执行高级方法
     * @param $method
     * @param $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        return call_user_func_array(array($this->handler(), $method), $parameters);
    }

Look at the test code

<?php

namespace app\index\controller;

use think\cache\driver\Redis;
use think\Controller;

class RedisTest extends Controller
{
    public function index()
    {
        $redis = new Redis();
//        $redis = $redis->handler();

        dump($redis->hSet('h_name', '2', 'jerry'));// int(1)
    }
}

This problem has been solved. When I finished the modification, I remembered that Laravel seemed to use __call, and then I looked at the source code, and it was indeed the case. There is the following code in ravel/vendor/laravel/framework/src/Illuminate/Redis/RedisManager.php

     /**
     * Pass methods onto the default Redis connection.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        return $this->connection()->{$method}(...$parameters);
    }

Conclusion

In fact, the symbolic meaning of this small modification Greater than practical significance, after all, this is not a bug, it can also be achieved using handler. The greater significance to me is that when encountering some problems, I will be more inclined to check the source code. The more you watch, your abilities will naturally improve.

The above is the detailed content of Introduction to the method of modifying Cache source code in ThinkPHP 5.1 (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete

Related articles

See more