首页 后端开发 php教程 构建自各儿的PHP框架-创建组件的机制

构建自各儿的PHP框架-创建组件的机制

Jun 13, 2016 pm 12:29 PM
config pdo php static

构建自己的PHP框架--创建组件的机制

在之前的博客中,我们完成了基本的Model类,但是大家应该还记得,我们创建数据库的pdo实例时,是hard好的配置,并且直接hard在Model类中。

代码如下:

<code class="sourceCode php">    <span class="kw">public</span> <span class="kw">static</span> <span class="kw">function</span> getDb<span class="ot">()</span>    {        <span class="kw">if</span> <span class="ot">(</span><span class="fu">empty</span><span class="ot">(</span><span class="kw">static</span>::<span class="kw">$pdo</span><span class="ot">))</span> {            <span class="kw">$host</span> = <span class="st">&#39;localhost&#39;</span><span class="ot">;</span>            <span class="kw">$database</span> = <span class="st">&#39;sf&#39;</span><span class="ot">;</span>            <span class="kw">$username</span> = <span class="st">&#39;jun&#39;</span><span class="ot">;</span>            <span class="kw">$password</span> = <span class="st">&#39;jun&#39;</span><span class="ot">;</span>            <span class="kw">$options</span> = <span class="ot">[</span>                <span class="kw">PDO</span>::<span class="kw">ATTR_EMULATE_PREPARES</span> => <span class="kw">false</span><span class="ot">,</span>                <span class="kw">PDO</span>::<span class="kw">ATTR_STRINGIFY_FETCHES</span> => <span class="kw">false</span>            <span class="ot">];</span>            <span class="kw">static</span>::<span class="kw">$pdo</span> = <span class="kw">new</span> <span class="kw">PDO</span><span class="ot">(</span><span class="st">"mysql:host=</span><span class="kw">$host</span><span class="st">;dbname=</span><span class="kw">$database</span><span class="st">"</span><span class="ot">,</span> <span class="kw">$username</span><span class="ot">,</span> <span class="kw">$password</span><span class="ot">,</span> <span class="kw">$options</span><span class="ot">);</span>            <span class="kw">static</span>::<span class="kw">$pdo</span>-><span class="fu">exec</span><span class="ot">(</span><span class="st">"set names &#39;utf8&#39;"</span><span class="ot">);</span>        }        <span class="kw">return</span> <span class="kw">static</span>::<span class="kw">$pdo</span><span class="ot">;</span>    }</code>
登录后复制

这明显是不合适的,修改的时候需要取修改框架的代码。现在我们就来将它抽出来。

首先我们需要将配置拿到相应的配置文件中,我们在simple-framework文件夹下创建一个config文件夹,然后添加一个db.php文件。在里面添加相关的配置。

代码如下:

<code class="sourceCode php"><span class="kw">return</span> <span class="ot">[</span>    <span class="st">&#39;class&#39;</span> => <span class="st">&#39;\PDO&#39;</span><span class="ot">,</span>    <span class="st">&#39;dsn&#39;</span> => <span class="st">&#39;mysql:host=localhost;dbname=sf&#39;</span><span class="ot">,</span>    <span class="st">&#39;username&#39;</span> => <span class="st">&#39;jun&#39;</span><span class="ot">,</span>    <span class="st">&#39;password&#39;</span> => <span class="st">&#39;jun&#39;</span><span class="ot">,</span>    <span class="st">&#39;options&#39;</span> => <span class="ot">[</span>        \<span class="kw">PDO</span>::<span class="kw">ATTR_EMULATE_PREPARES</span> => <span class="kw">false</span><span class="ot">,</span>        \<span class="kw">PDO</span>::<span class="kw">ATTR_STRINGIFY_FETCHES</span> => <span class="kw">false</span><span class="ot">,</span>    <span class="ot">],</span><span class="ot">];</span></code>
登录后复制

其实就是返回一个数组,数组中是我们创建pdo实例所需要的参数。在创建pdo实例之前,将db.php文件require进来即可。

代码如下:

<code class="sourceCode php">    <span class="kw">public</span> <span class="kw">static</span> <span class="kw">function</span> getDb<span class="ot">()</span>    {        <span class="kw">if</span> <span class="ot">(</span><span class="fu">empty</span><span class="ot">(</span><span class="kw">static</span>::<span class="kw">$pdo</span><span class="ot">))</span> {            <span class="kw">$config</span> = <span class="kw">require</span><span class="ot">(</span><span class="st">&#39;../config/db.php&#39;</span><span class="ot">);</span>            <span class="kw">static</span>::<span class="kw">$pdo</span> = <span class="kw">new</span> <span class="kw">$config</span><span class="ot">[</span><span class="st">&#39;class&#39;</span><span class="ot">](</span><span class="kw">$config</span><span class="ot">[</span><span class="st">&#39;dsn&#39;</span><span class="ot">],</span> <span class="kw">$config</span><span class="ot">[</span><span class="st">&#39;username&#39;</span><span class="ot">],</span> <span class="kw">$config</span><span class="ot">[</span><span class="st">&#39;password&#39;</span><span class="ot">],</span> <span class="kw">$config</span><span class="ot">[</span><span class="st">&#39;options&#39;</span><span class="ot">]);</span>            <span class="kw">static</span>::<span class="kw">$pdo</span>-><span class="fu">exec</span><span class="ot">(</span><span class="st">"set names &#39;utf8&#39;"</span><span class="ot">);</span>        }        <span class="kw">return</span> <span class="kw">static</span>::<span class="kw">$pdo</span><span class="ot">;</span>    }</code>
登录后复制

这样还是有点不爽,在require配置文件的时候,我需要一层层的去看相对位置,如果错了,就找不到了。而且将来不管是配置文件还是Model文件的位置变动了,相对位置就变了,我都需要修改这段代码。如果改成绝对地址是不是会好一些,至少配置文件位置不变,就不需要修改这段代码。

但是要改成绝对地址,我就需要定位simple-framework文件夹的地址,所以我们在public/index.php中define一个常量,标记simple-framework文件夹的地址。

public/index.php代码如下:

<code class="sourceCode php"><span class="kw"><?php</span><span class="fu">define</span><span class="ot">(</span><span class="st">&#39;SF_PATH&#39;</span><span class="ot">,</span> <span class="fu">dirname</span><span class="ot">(</span><span class="kw">__DIR__</span><span class="ot">));</span><span class="kw">require_once</span><span class="ot">(</span><span class="kw">SF_PATH</span> . <span class="st">&#39;/vendor/autoload.php&#39;</span><span class="ot">);</span><span class="kw">$application</span> = <span class="kw">new</span> sf\web\Application<span class="ot">();</span><span class="kw">$application</span>->run<span class="ot">();</span></code>
登录后复制

定义了常量SF_PATH,Model中的require可改为如下代码:

<code class="sourceCode php"><span class="kw">$config</span> = <span class="kw">require</span><span class="ot">(</span><span class="kw">SF_PATH</span> . <span class="st">&#39;/config/db.php&#39;</span><span class="ot">);</span></code>
登录后复制

OK,这样看起来好多了。

但是我又想到,假设将来我又需要一个cache的实例,我是不是又要把getDb的方法写一遍呢?如果要避免这种情况,我是不是应该把它抽想出来呢?那要如何抽象呢?

这个时候我们可能需要有一个方法,我们只需要告诉它我们需要创建一个什么实例,然后它就自己去取相应的参数,并创建出相应的实例来。这样是不是很爽。

我们需要在src文件夹下创建一个Sf.php,在其中创建一个createObject方法,用来创建实例。

但这个时候我们又遇到一个问题,pdo实例创建的时候,要按一定的顺序传一定个数的参数,但我们之后需要创建的其它的实例,可能传递的参数的个数和key都不同,那我们该怎么办?

所以我们需要一种机制能够将参数存起来,真正创建实例时,再取出来使用。我们可以在src/db文件夹下创建一个Connection类,Sf中的createObject方法创建了这个类的实例,这个类提供一个创建pdo实例的方法即可。

Sf.php中的代码如下:

<code class="sourceCode php"><span class="kw"><?php</span><span class="co">/**</span><span class="co"> * Sf is a helper class serving common framework functionalities.</span><span class="co"> * </span><span class="kw">@author</span><span class="co"> Harry Sun </span><span class="kw"><sunguangjun</span><span class="ot">@126.com</span><span class="kw">></span><span class="co"> */</span><span class="kw">class</span> Sf{    <span class="co">/**</span><span class="co">     * Creates a new object using the given configuration.</span><span class="co">     * You may view this method as an enhanced version of the `new` operator.</span><span class="co">     * </span><span class="kw">@param</span><span class="co"> </span><span class="kw">string</span><span class="co"> $name the object name</span><span class="co">     */</span>    <span class="kw">public</span> <span class="kw">static</span> <span class="kw">function</span> createObject<span class="ot">(</span><span class="kw">$name</span><span class="ot">)</span>    {        <span class="kw">$config</span> = <span class="kw">require</span><span class="ot">(</span><span class="kw">SF_PATH</span> . <span class="st">"/config/</span><span class="kw">$name</span><span class="st">.php"</span><span class="ot">);</span>        <span class="co">// create instance</span>        <span class="kw">$instance</span> = <span class="kw">new</span> <span class="kw">$config</span><span class="ot">[</span><span class="st">&#39;class&#39;</span><span class="ot">]();</span>        <span class="fu">unset</span><span class="ot">(</span><span class="kw">$config</span><span class="ot">[</span><span class="st">&#39;class&#39;</span><span class="ot">]);</span>        <span class="co">// add attributes</span>        <span class="kw">foreach</span> <span class="ot">(</span><span class="kw">$config</span> <span class="kw">as</span> <span class="kw">$key</span> => <span class="kw">$value</span><span class="ot">)</span> {            <span class="kw">$instance</span>-><span class="kw">$key</span> = <span class="kw">$value</span><span class="ot">;</span>        }        <span class="kw">return</span> <span class="kw">$instance</span><span class="ot">;</span>    }}</code>
登录后复制

Sf类没有namespace,所以不符合psr4的规则,需要手动引入,在public/index.php中加入如下一句话:

<code class="sourceCode php"><span class="kw">require_once</span><span class="ot">(</span><span class="kw">SF_PATH</span> . <span class="st">&#39;/src/Sf.php&#39;</span><span class="ot">);</span></code>
登录后复制

Connection.php中的代码如下:

<code class="sourceCode php"><span class="kw"><?php</span><span class="kw">namespace</span> sf\db<span class="ot">;</span><span class="kw">use</span> <span class="kw">PDO</span><span class="ot">;</span><span class="co">/**</span><span class="co"> * Connection represents a connection to a database via [PDO](php.net/manual/en/book.pdo.php).</span><span class="co"> * </span><span class="kw">@author</span><span class="co"> Harry Sun </span><span class="kw"><sunguangjun</span><span class="ot">@126.com</span><span class="kw">></span><span class="co"> */</span><span class="kw">class</span> Connection{    <span class="co">/**</span><span class="co">     * </span><span class="kw">@var</span><span class="co"> </span><span class="st">string</span><span class="co"> </span><span class="st">the</span><span class="co"> </span><span class="st">Data</span><span class="co"> </span><span class="st">Source</span><span class="co"> </span><span class="st">Name,</span><span class="co"> </span><span class="st">or</span><span class="co"> </span><span class="st">DSN,</span><span class="co"> </span><span class="st">contains</span><span class="co"> </span><span class="st">the</span><span class="co"> </span><span class="st">information</span><span class="co"> </span><span class="st">required</span><span class="co"> </span><span class="st">to</span><span class="co"> </span><span class="st">connect</span><span class="co"> </span><span class="st">to</span><span class="co"> </span><span class="st">the</span><span class="co"> </span><span class="st">database.</span><span class="co">     * Please refer to the [PHP manual](http://www.php.net/manual/en/function.PDO-construct.php) on</span><span class="co">     * the format of the DSN string.</span><span class="co">     * </span><span class="kw">@see</span><span class="co"> charset</span><span class="co">     */</span>    <span class="kw">public</span> <span class="kw">$dsn</span><span class="ot">;</span>    <span class="co">/**</span><span class="co">     * </span><span class="kw">@var</span><span class="co"> </span><span class="st">string</span><span class="co"> </span><span class="st">the</span><span class="co"> </span><span class="st">username</span><span class="co"> </span><span class="st">for</span><span class="co"> </span><span class="st">establishing</span><span class="co"> </span><span class="st">DB</span><span class="co"> </span><span class="st">connection.</span><span class="co"> </span><span class="st">Defaults</span><span class="co"> </span><span class="st">to</span><span class="co"> </span><span class="st">`null`</span><span class="co"> </span><span class="st">meaning</span><span class="co"> </span><span class="st">no</span><span class="co"> </span><span class="st">username</span><span class="co"> </span><span class="st">to</span><span class="co"> </span><span class="st">use.</span><span class="co">     */</span>    <span class="kw">public</span> <span class="kw">$username</span><span class="ot">;</span>    <span class="co">/**</span><span class="co">     * </span><span class="kw">@var</span><span class="co"> </span><span class="st">string</span><span class="co"> </span><span class="st">the</span><span class="co"> </span><span class="st">password</span><span class="co"> </span><span class="st">for</span><span class="co"> </span><span class="st">establishing</span><span class="co"> </span><span class="st">DB</span><span class="co"> </span><span class="st">connection.</span><span class="co"> </span><span class="st">Defaults</span><span class="co"> </span><span class="st">to</span><span class="co"> </span><span class="st">`null`</span><span class="co"> </span><span class="st">meaning</span><span class="co"> </span><span class="st">no</span><span class="co"> </span><span class="st">password</span><span class="co"> </span><span class="st">to</span><span class="co"> </span><span class="st">use.</span><span class="co">     */</span>    <span class="kw">public</span> <span class="kw">$password</span><span class="ot">;</span>    <span class="co">/**</span><span class="co">     * </span><span class="kw">@var</span><span class="co"> </span><span class="st">array</span><span class="co"> </span><span class="st">PDO</span><span class="co"> </span><span class="st">attributes</span><span class="co"> </span><span class="st">(name</span><span class="co"> </span><span class="st">=></span><span class="co"> </span><span class="st">value)</span><span class="co">     * to establish a DB connection. Please refer to the</span><span class="co">     * [PHP manual](http://www.php.net/manual/en/function.PDO-setAttribute.php) for</span><span class="co">     * details about available attributes.</span><span class="co">     */</span>    <span class="kw">public</span> <span class="kw">$attributes</span><span class="ot">;</span>    <span class="kw">public</span> <span class="kw">function</span> getDb<span class="ot">()</span>    {        <span class="kw">return</span> <span class="kw">new</span> <span class="kw">PDO</span><span class="ot">(</span><span class="kw">$this</span>->dsn<span class="ot">,</span> <span class="kw">$this</span>->username<span class="ot">,</span> <span class="kw">$this</span>->password<span class="ot">,</span> <span class="kw">$this</span>->attributes<span class="ot">);</span>    }}</code>
登录后复制

虽然不显示声明dsn/username/password和attributes属性也是可行的,但我觉得还是声明出来比较好,这样大家看到这个类的时候,会知道有哪些属性。

Model类中的getDb方法改成如下代码:

<code class="sourceCode php">    <span class="kw">public</span> <span class="kw">static</span> <span class="kw">function</span> getDb<span class="ot">()</span>    {        <span class="kw">if</span> <span class="ot">(</span><span class="fu">empty</span><span class="ot">(</span><span class="kw">static</span>::<span class="kw">$pdo</span><span class="ot">))</span> {            <span class="kw">static</span>::<span class="kw">$pdo</span> = Sf::createObject<span class="ot">(</span><span class="st">&#39;db&#39;</span><span class="ot">)</span>->getDb<span class="ot">();</span>            <span class="kw">static</span>::<span class="kw">$pdo</span>-><span class="fu">exec</span><span class="ot">(</span><span class="st">"set names &#39;utf8&#39;"</span><span class="ot">);</span>        }        <span class="kw">return</span> <span class="kw">static</span>::<span class="kw">$pdo</span><span class="ot">;</span>    }</code>
登录后复制

不要忘记需要use一下Sf。

config中的db.php也要跟着修改,代码如下:

<code class="sourceCode php"><span class="kw"><?php</span><span class="kw">return</span> <span class="ot">[</span>    <span class="st">&#39;class&#39;</span> => <span class="st">&#39;\sf\db\Connection&#39;</span><span class="ot">,</span>    <span class="st">&#39;dsn&#39;</span> => <span class="st">&#39;mysql:host=localhost;dbname=sf&#39;</span><span class="ot">,</span>    <span class="st">&#39;username&#39;</span> => <span class="st">&#39;jun&#39;</span><span class="ot">,</span>    <span class="st">&#39;password&#39;</span> => <span class="st">&#39;jun&#39;</span><span class="ot">,</span>    <span class="st">&#39;attributes&#39;</span> => <span class="ot">[</span>        \<span class="kw">PDO</span>::<span class="kw">ATTR_EMULATE_PREPARES</span> => <span class="kw">false</span><span class="ot">,</span>        \<span class="kw">PDO</span>::<span class="kw">ATTR_STRINGIFY_FETCHES</span> => <span class="kw">false</span><span class="ot">,</span>    <span class="ot">],</span><span class="ot">];</span></code>
登录后复制

我默默的将options换成了attributes,毕竟在PDO的文档里,写的是attribute。

好了,今天就先到这里。项目内容和博客内容也都会放到Github上,欢迎大家提建议。

code:https://github.com/CraryPrimitiveMan/simple-framework/tree/0.8

blog project:https://github.com/CraryPrimitiveMan/create-your-own-php-framework

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

CakePHP 项目配置 CakePHP 项目配置 Sep 10, 2024 pm 05:25 PM

在本章中,我们将了解CakePHP中的环境变量、常规配置、数据库配置和电子邮件配置。

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 日期和时间 CakePHP 日期和时间 Sep 10, 2024 pm 05:27 PM

为了在 cakephp4 中处理日期和时间,我们将使用可用的 FrozenTime 类。

CakePHP 文件上传 CakePHP 文件上传 Sep 10, 2024 pm 05:27 PM

为了进行文件上传,我们将使用表单助手。这是文件上传的示例。

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

在本章中,我们将学习以下与路由相关的主题?

讨论 CakePHP 讨论 CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 Dec 20, 2024 am 11:31 AM

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

CakePHP 创建验证器 CakePHP 创建验证器 Sep 10, 2024 pm 05:26 PM

可以通过在控制器中添加以下两行来创建验证器。

See all articles