I have been working for half a year, and I feel that I have learned more in this half year than in four years of college. The main reason is that my mind has calmed down, my goals are clear, and I will not be entangled with games all day long. When I was in college, I actually realized that playing games would affect my normal study and work, but I couldn't control myself. I still couldn't help but play often, day and night (I was originally a sultry guy, and I still stayed at home playing games. , this is also one of the reasons why I only had games, left and right hands, and no girlfriends during the four years of college). Now that I am working, I have tasks every day. When I see the talented people next to me working on the project like a duck to water, I have the idea of catching up with them, so I give myself an extra small task every day to learn new knowledge. I have been working for half a year now. Now I can use Linux which I was not familiar with before. I also have a new understanding of JS which I am not familiar with. It can be said that I am competent at the job now (if divided into novice, advanced novice, competent and proficient) , experts), have developed activities, interfaces, and backends, and have also optimized and improved the system framework. As long as there are reasonable needs raised by product operations, they can be quickly supported. Of course, I really feel one thing: Programmers are really a strange group. Most of the time, they always think that their own ideas are the best. Of course, this is considered self-confidence, but sometimes your aggressiveness during discussions is not necessarily a good thing, so you should listen to other people's ideas. Not only can you discover your own shortcomings, but you can also build a good "friendship". I’ve been telling you so much about my feelings over the past six months, thank you for sticking with me and reading it^_^.
Let’s step into the real question and talk about how to skillfully use the magic method of PHP. I believe this will be used in most projects.
First of all, I would like to explain that this little trick has been used very well in my project, and it has brought great convenience to our project. I will give you some details here, you may wish to continue reading.
In the project, there must be a large amount of configuration information that can be configured, such as the robot opening time period of a game, whether the payment method is enabled, the configuration of the title display in the mall, etc. One characteristic of these configuration information is that there is no specific Rules, and product operations can be modified at any time according to the actual situation. How to save this information? It is definitely not possible to build a table for each type. Doing so is simply thankless. If you think about it, maybe one table can A piece of information is saved, so I have to think of other methods. Although there are no rules for this information, one of their characteristics is that there will not be too much, and generally an array can save all the information that needs to be configured, so json characters are used String storage information is a good choice. When you need to use it, just take out the json_decode and you can use it directly. Let’s take a look at how to cleverly use PHP’s magic method to achieve it.
Here you first need to understand __call(), a magic method of PHP. Check the official PHP documentation, which explains this function like this
<span>public</span> <span>mixed</span> __call ( <span>string</span> <span>$name</span> , <span>array</span> <span>$arguments</span><span> ) __call() is triggered when invoking inaccessible methods in an </span><span>object</span> context.
It means that this function will be triggered when an inaccessible method (no permission, non-existence) is called in an object. The parameter $name of the function is the name of the called function, and $arguments is the parameter array of the called function. Take a look at this example:
<span>class</span><span> Test { </span><span>public</span> <span>function</span> __call(<span>$name</span>, <span>$arguments</span><span>) { </span><span>echo</span> "你调用了一个不存在的方法:\r"<span>; </span><span>echo</span> "函数名:{<span>$name</span>}\r"<span>; </span><span>echo</span> "参数: \r"<span>; </span><span>print_r</span>(<span>$arguments</span><span>); } } </span><span>$T</span> = <span>new</span><span> Test(); </span><span>$T</span>->setrobottime("12", "18");
This function will output the following results
<span>你调用了一个不存在的方法: 函数名:setrobottime 参数: Array ( [</span>0] => 12<span> [</span>1] => 18<span> )</span>
In this way, we can not define the function directly, but use this feature to do something. Let’s take a look at the implementation ideas of the code, mainly the ideas, some of which I have made assumptions about, such as database connections, which I won’t focus on here.
<span>class</span><span> Config { </span><span>/*</span><span>* * 这里假定下数据库表名为 * config.config, * 字段为: * config_key varchar(50), * config_value text, * primary key(config_key) * * 数据库连接为$link * 插入方法封装为query * 获取一条信息方法封装为getOne </span><span>*/</span> <span>/*</span><span>* * 要进行的操作 </span><span>*/</span> <span>private</span> <span>static</span> <span>$keys</span> = <span>array</span><span>( </span><span>//</span><span>'调用方法' => 'key',</span> 'roboottime' => 'ROBOOTTIME', 'dailysignin' => 'DAILYSIGNIN',<span> ); </span><span>/*</span><span>* * 设置方法 * @param string $config_key 配置项key * @param string $config_value 配置型内容(一般为json格式) * @returne boolen true/false 插入是否成功 </span><span>*/</span> <span>private</span> <span>function</span> set(<span>$config_key</span>, <span>$config_value</span><span>){ </span><span>$sql</span> = "insert into config.config (config_key,config_value) values ('{<span>$config_key</span>}','{<span>$config_value</span>}') on duplicate key update config_value='{<span>$config_value</span>}'"<span>; </span><span>return</span> <span>$link</span>->query(<span>$sql</span><span>); } </span><span>/*</span><span>* * 获取值的方法 * @param $config_key 要获取的配置的key * @returne string/false json字符串/失败 </span><span>*/</span> <span>private</span> <span>function</span> get(<span>$config_key</span><span>) { </span><span>$sql</span> = "select * from config.config where config_key='{<span>$config_key</span>}'"<span>; </span><span>if</span>(<span>$ret</span> = <span>$link</span>->getOne(<span>$sql</span>,<span> MYSQL_ASSOC)){ </span><span>return</span> <span>$ret</span><span>; } </span><span>return</span> <span>false</span><span>; } </span><span>/*</span><span>* * 重载魔术方法 * @param string $name 被调用的方法名 * @param array $arguments 调用时传递的参数 * @return mixed 返回结果 </span><span>*/</span> <span>public</span> <span>function</span> __call(<span>$name</span>, <span>$arguments</span><span>) { </span><span>$act</span> = <span>substr</span>(<span>$name</span>, 0, 3<span>); </span><span>$func</span> = <span>substr</span>(<span>$name</span>, 3<span>); </span><span>if</span>(!<span>in_array</span>(<span>$func</span>, self::<span>$keys</span><span>)){ </span><span>return</span> <span>false</span><span>; } </span><span>if</span>(<span>$act</span> == 'set'<span>) { </span><span>return</span> <span>$this</span>->set(self::[<span>$func</span>], <span>$arguments</span>[0<span>]); } </span><span>elseif</span>(<span>$act</span> == 'get'<span>) { </span><span>return</span> <span>$this</span>->get(self::[<span>$func</span><span>]); } </span><span>return</span> <span>false</span><span>; } }</span>
In this way, we can store multiple information through one table, and it is very convenient when calling. We only need to expand the information in the Config::$keys array. This is just for standardization and clarity. Know which configurations are stored in this table.
You can store and retrieve it like this when using it
<span>$config</span> = <span>new</span><span> Config(); </span><span>$info</span> = <span>array</span>("12","20"<span>); </span><span>//</span><span>设置</span> <span>$config</span>->setroboottime(json_encode(<span>$info</span><span>)); </span><span>//</span><span>获取</span> <span>$config</span>->getroboottime();
Another point to note here is that these configuration information will generally be cached in redis and placed in the database just to prevent recovery from the database after redis hangs. This generally refers to information that is frequently read. In order to reduce the interaction with the db, place it directly in the cache.
The copyright of this article belongs to the author iforever (luluyrt@163.com). Any form of reprinting is prohibited without the author's consent. After reprinting the article, the author and the original text link must be provided in an obvious position on the article page, otherwise the right to pursue legal liability is reserved. .
Refresh comments Refresh page Return to top
Blog Park Homepage Bowen News Flash Programmer Recruitment Knowledge Base
公告
Copyright ©2015 奔跑的Man

SQL中的Identity是什么,需要具体代码示例在SQL中,Identity是一种用于生成自增数字的特殊数据类型,它常用于唯一标识表中的每一行数据。Identity列通常与主键列配合使用,可以确保每条记录都有一个独一无二的标识符。本文将详细介绍Identity的使用方式以及一些实际的代码示例。Identity的基本使用方式在创建表时,可以使用Identit

一、功能概览键空间通知使得客户端可以通过订阅频道或模式,来接收那些以某种方式改动了Rediskey变化的事件。所有修改key键的命令。所有接收到LPUSHkeyvalue[value…]命令的键。db数据库中所有已过期的键。事件通过Redis的订阅与发布功能(pub/sub)来进行分发,因此所有支持订阅与发布功能的客户端都可以在无须做任何修改的情况下,直接使用键空间通知功能。因为Redis目前的订阅与发布功能采取的是发送即忘(fireandforget)策略,所以如果你的程

function是函数的意思,是一段具有特定功能的可重复使用的代码块,是程序的基本组成单元之一,可以接受输入参数,执行特定的操作,并返回结果,其目的是封装一段可重复使用的代码,提高代码的可重用性和可维护性。

在安装应用程序的新版本时,Windows可能会显示此错误消息“解析C:\\Windows\Microsoft.Net\Framework\v2.0.50727\Config\machine.configParser返回错误0xC00CE556时出错”。当您的系统启动时,这个问题也会出现。无论您在何种情况下遇到此问题,.NETFramework都是幕后真正的罪魁祸首。您可以使用一些非常简单的修复程序来阻止此错误代码再次出现。修复1–替换损坏的文件您可以轻松地从原始目录中替换损坏的ma

An unpatchable Yubico two-factor authentication key vulnerability has broken the security of most Yubikey 5, Security Key, and YubiHSM 2FA devices. The Feitian A22 JavaCard and other devices using Infineon SLB96xx series TPMs are also vulnerable.All

遇到的问题:在开发过程中,会遇到要批量删除某种规则的key,例如login_logID(ID为变量),现在需要删除"login_log*"这一类的数据,但是redis本身只有批量查询一类key值的命令keys,但是没有批量删除某一个类的命令。解决办法:先查询,在删除,使用xargs传参(xargs可以将管道或标准输入(stdin)数据转换成命令行参数),先执行查询语句,在将查询出来的key值,当初del的参数去删除。redis-cliKEYSkey*(查找条件)|xargsr

随着PHP语言的不断发展,做为PHP后端框架中广泛使用的ThinkPHP也在不断完善。随着业务场景的逐渐复杂,ThinkPHP中对于配置管理的需求也越来越大。在这种背景下,ThinkPHP提供了丰富的配置管理功能,今天我们就来介绍一下如何通过ThinkPHPConfig实现配置管理。一、ThinkPHPConfig的介绍ThinkPHPConfig是Thin

MySQL.proc表的作用和功能详解MySQL是一种流行的关系型数据库管理系统,开发者在使用MySQL时常常会涉及到存储过程(StoredProcedure)的创建和管理。而MySQL.proc表则是一个非常重要的系统表,它存储了数据库中所有的存储过程的相关信息,包括存储过程的名称、定义、参数等。在本文中,我们将详细解释MySQL.proc表的作用和功能


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
