Home > PHP Framework > ThinkPHP > 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

藏色散人
Release: 2021-03-15 09:03:07
forward
1924 people have browsed it

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 <div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">Copy after login</div></div>, 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<div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">Copy after login</div></div>Look at the test code<div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">Copy after login</div></div>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

<div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">Copy after login</div></div>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!

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