Home  >  Article  >  PHP Framework  >  Detailed explanation of how to modify Cache source code in ThinkPHP 5.1

Detailed explanation of how to modify Cache source code in ThinkPHP 5.1

藏色散人
藏色散人forward
2021-03-12 11:37:241878browse

The following tutorial column will give you a detailed explanation of how to modify the Cache source code of ThinkPHP 5.1. I hope it will be helpful to friends in need!

ThinkPHP 5.1 Modify Cache source codeDetailed explanation of how to modify Cache source code in ThinkPHP 5.1

Introduction

I am learning THinkPHP 5.1 recently and read it The operation of the Cache method is a bit confusing. 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 of all, I would like to declare 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
modify the source code

.

QuestionWhile practicing Redis, I found that if you want to use advanced methods, such as

hSet

,

hGet

, etc., you must first return the handle, Then it can be executed. As you can see below <pre class="brush:php;toolbar:false">&lt;?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-&gt;handler();         dump($redis-&gt;hSet('h_name', '1', 'tom'));// int(1)     } }</pre>, the execution was successful. The question is why the handle should be returned first, which can be solved using the magic method __call.

Tracking source codeSince you have doubts, you must clear them up. Tracking 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. Solving the problemThe solution is to add the

__call

method in

thinkphp/library/think/cache/Driver.php

, so that not only Redis can Use the high-level method directly, and other Cache classes that inherit this file can use it directly. The code is as follows<pre class="brush:php;toolbar:false">     /**      * 执行高级方法      * @param $method      * @param $parameters      * @return mixed      */     public function __call($method, $parameters)     {         return call_user_func_array(array($this-&gt;handler(), $method), $parameters);     }</pre>Look at the test code<pre class="brush:php;toolbar:false">&lt;?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-&gt;handler();         dump($redis-&gt;hSet('h_name', '2', 'jerry'));// int(1)     } }</pre>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

<pre class="brush:php;toolbar:false">     /**      * Pass methods onto the default Redis connection.      *      * @param  string  $method      * @param  array  $parameters      * @return mixed      */     public function __call($method, $parameters)     {         return $this-&gt;connection()-&gt;{$method}(...$parameters);     }</pre>ConclusionIn 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 Detailed explanation of how to modify Cache source code in ThinkPHP 5.1. 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