<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>PHP on 小凡的博客</title><link>https://www.rvp.cc/tags/php/</link><description>Recent content in PHP on 小凡的博客</description><generator>Hugo</generator><language>zh-CN</language><lastBuildDate>Thu, 08 Jun 2023 17:03:44 +0800</lastBuildDate><atom:link href="https://www.rvp.cc/tags/php/index.xml" rel="self" type="application/rss+xml"/><item><title>PHP设计模式之——依赖注入</title><link>https://www.rvp.cc/posts/php-dependency-injection/</link><pubDate>Thu, 08 Jun 2023 17:03:44 +0800</pubDate><guid>https://www.rvp.cc/posts/php-dependency-injection/</guid><description>&lt;p&gt;依赖注入(dependency injection)是实现控制反转的一种技术，顾名思义，就是将依赖(被调用者)注入给依赖对象(调用者)。&lt;/p&gt;&#10;&lt;p&gt;用户类&lt;code&gt;User&lt;/code&gt;有一个注册方法，我们需要实现一个功能，在注册成功后，给用户发送一封邮件通知。我们先写一个发送邮件的类：&lt;/p&gt;&#10;&lt;div class="code-block-wrap"&gt;&#10; &lt;div class="code-toolbar"&gt;&#10; &lt;span class="code-language" aria-hidden="true"&gt;php&lt;/span&gt;&#10; &lt;button class="code-copy" type="button" aria-label="复制代码" title="复制代码" data-code-copy hidden&gt;&#10; &lt;span class="code-copy-icon" aria-hidden="true"&gt;&#10; &lt;svg class="copy-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" focusable="false"&gt;&lt;rect x="8" y="8" width="12" height="12" rx="2"/&gt;&lt;path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2"/&gt;&lt;/svg&gt;&#10; &lt;svg class="check-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" focusable="false"&gt;&lt;path d="m20 6-11 11-5-5"/&gt;&lt;/svg&gt;&#10; &lt;/span&gt;&#10; &lt;span data-copy-label&gt;复制&lt;/span&gt;&#10; &lt;/button&gt;&#10; &lt;/div&gt;&#10; &lt;pre class="code-block" tabindex="0" data-language="php"&gt;&lt;code class="language-php"&gt;class EmailClass&#10;{&#10; public function sendEmail($username, $email)&#10; {&#10; print_r(&amp;#34;send register email to:&amp;#34; . $email . PHP_EOL);&#10; }&#10;}&lt;/code&gt;&lt;/pre&gt;&#10; &lt;/div&gt;&lt;p&gt;然后在用户注册方法中实例化一个邮件类：&lt;/p&gt;&#10;&lt;div class="code-block-wrap"&gt;&#10; &lt;div class="code-toolbar"&gt;&#10; &lt;span class="code-language" aria-hidden="true"&gt;php&lt;/span&gt;&#10; &lt;button class="code-copy" type="button" aria-label="复制代码" title="复制代码" data-code-copy hidden&gt;&#10; &lt;span class="code-copy-icon" aria-hidden="true"&gt;&#10; &lt;svg class="copy-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" focusable="false"&gt;&lt;rect x="8" y="8" width="12" height="12" rx="2"/&gt;&lt;path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2"/&gt;&lt;/svg&gt;&#10; &lt;svg class="check-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" focusable="false"&gt;&lt;path d="m20 6-11 11-5-5"/&gt;&lt;/svg&gt;&#10; &lt;/span&gt;&#10; &lt;span data-copy-label&gt;复制&lt;/span&gt;&#10; &lt;/button&gt;&#10; &lt;/div&gt;&#10; &lt;pre class="code-block" tabindex="0" data-language="php"&gt;&lt;code class="language-php"&gt;class User&#10;{&#10; private $username;&#10; private $password;&#10; private $mobile;&#10; private $email;&#10;&#10; public function register()&#10; {&#10; // 注册逻辑&#10; // .........&#10;&#10; // 发送邮件逻辑&#10; $emailClass = new EmailClass();&#10; $emailClass-&amp;gt;sendEmail($this-&amp;gt;username, $this-&amp;gt;email);&#10; }&#10;}&lt;/code&gt;&lt;/pre&gt;&#10; &lt;/div&gt;&lt;p&gt;这时候又来一个需求，需要在注册成功之后给用户发送短信，于是又要在注册方法里面实例化一个发送短信的类。或者这个时候我们要改一下邮件内容，要把注册密码发给用户，于是我们要给&lt;code&gt;EmailClass::sendEmail&lt;/code&gt;加一个password参数。经过长期迭代和添油加醋，我们需要频繁的去修改核心注册逻辑，最终的注册方法将变得极为臃肿且难以维护。&lt;/p&gt;</description></item><item><title>Symfony Cache报错Cache key 'xxx:xxx' contains reserved characters解决办法</title><link>https://www.rvp.cc/posts/symfony-cache-reserved-characters/</link><pubDate>Fri, 02 Jun 2023 14:57:04 +0800</pubDate><guid>https://www.rvp.cc/posts/symfony-cache-reserved-characters/</guid><description>&lt;p&gt;今天在使用Symfony Cache时遇到一个报错：&lt;code&gt;Symfony\Component\Cache\Exception\InvalidArgumentException: Cache key &amp;quot;xxx:xxx&amp;quot; contains reserved characters &amp;quot;{}()/\@:&amp;quot;&lt;/code&gt;，经过排查发现我的缓存key带有冒号。&lt;/p&gt;</description></item><item><title>一次诡异的PHP浮点运算精度丢失以及打印问题</title><link>https://www.rvp.cc/posts/php-floating-point-precision/</link><pubDate>Wed, 31 May 2023 15:46:51 +0800</pubDate><guid>https://www.rvp.cc/posts/php-floating-point-precision/</guid><description>&lt;p&gt;项目开发时发现几个小数组成的数组进行求和的结果不对，第一反应就觉得是精度问题，于是做了实验：&lt;/p&gt;&#10;&lt;div class="code-block-wrap"&gt;&#10; &lt;div class="code-toolbar"&gt;&#10; &lt;span class="code-language" aria-hidden="true"&gt;php&lt;/span&gt;&#10; &lt;button class="code-copy" type="button" aria-label="复制代码" title="复制代码" data-code-copy hidden&gt;&#10; &lt;span class="code-copy-icon" aria-hidden="true"&gt;&#10; &lt;svg class="copy-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" focusable="false"&gt;&lt;rect x="8" y="8" width="12" height="12" rx="2"/&gt;&lt;path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2"/&gt;&lt;/svg&gt;&#10; &lt;svg class="check-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" focusable="false"&gt;&lt;path d="m20 6-11 11-5-5"/&gt;&lt;/svg&gt;&#10; &lt;/span&gt;&#10; &lt;span data-copy-label&gt;复制&lt;/span&gt;&#10; &lt;/button&gt;&#10; &lt;/div&gt;&#10; &lt;pre class="code-block" tabindex="0" data-language="php"&gt;&lt;code class="language-php"&gt;$array = [0.4, 0.4, 0.2, 0.4, 0.4, 0.2];&#10;$arraySum = array_sum($array);&#10;var_dump($arraySum);&#10;var_dump(intval($arraySum));&#10;var_export($arraySum);&lt;/code&gt;&lt;/pre&gt;&#10; &lt;/div&gt;&lt;p&gt;猜猜以上代码运行的结果是什么？&lt;/p&gt;</description></item></channel></rss>