Home > Backend Development > PHP Tutorial > 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)

不言
Release: 2023-04-05 06:42:02
forward
2338 people have browsed it

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)
    }
}
Copy after login

, 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);
    }
Copy after login

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)
    }
}
Copy after login

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);
    }
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template