亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
搭建自己的PHP框架心得(二),搭建php框架心得
續(xù)言
回調(diào)函數(shù)
VIEW層和ob函數(shù)
類__URL__常量和全局類
用單例模式定義數(shù)據(jù)庫(kù)連接基類
DB類的sql查詢函數(shù)
后續(xù)
Home Backend Development PHP Tutorial Experience in building your own PHP framework (2), experience in building PHP framework_PHP tutorial

Experience in building your own PHP framework (2), experience in building PHP framework_PHP tutorial

Jul 12, 2016 am 08:55 AM
php

搭建自己的PHP框架心得(二),搭建php框架心得

續(xù)言

對(duì)于本次更新,我想說(shuō):

  • 本框架由本人挑時(shí)間完善,而我還不是PHP大神級(jí)的人物,所以框架漏洞難免,求大神們指出。
  • 本框架的知識(shí)點(diǎn)應(yīng)用都會(huì)寫(xiě)在博客里,大家有什么異議的可以一起討論,也希望看博客的也能學(xué)習(xí)到它們。
  • 本次更新,更新了函數(shù)規(guī)范上的一些問(wèn)題,如將函數(shù)盡量的獨(dú)立化,每一個(gè)函數(shù)盡量只單獨(dú)做好一件事情,盡量減少函數(shù)依賴。還對(duì)框架的整體優(yōu)化了一下,添加了SQ全局類,用以處理全局函數(shù),變量。

再次貼出GITHUB地址:Sqier框架GITHUB地址


回調(diào)函數(shù)

替換了很low的類名拼裝實(shí)例化,然后拼裝方法名的用法,使用PHP的回調(diào)函數(shù)方式:

原代碼:

<code>$controller_name = 'Controller\\' . self::$c_name;
$action_name = self::$a_name . 'Action';
$controller = new $controller_name();
$controller->$action_name();
</code>

修改后代碼

<code>    $controller_name = 'Controller\\' . self::$c_name;
    $controller = new $controller_name();
    call_user_func([
        $controller,
        self::$a_name . 'Action'
    ]);
</code>

這里介紹一下PHP的函數(shù)回調(diào)應(yīng)用方式:call_user_func和call_user_func_array:

<p>call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] )</p>
<p>調(diào)用第一個(gè)參數(shù)所提供的用戶自定義的函數(shù)。</p>
<p>返回值:返回調(diào)用函數(shù)的結(jié)果,或FALSE。</p>

call_user_func_array()的用法跟call_user_func類似,只不過(guò)傳入的參數(shù)params整體為一個(gè)數(shù)組。

另外,call_user_func系列函數(shù)還可以傳入在第一個(gè)參數(shù)里傳入匿名參數(shù),可以很方便的回調(diào)某些事件,這些特性在復(fù)雜的框架里應(yīng)用也十分廣泛,如yii2的事件機(jī)制里回調(diào)函數(shù)的使用就是基于此。


VIEW層和ob函數(shù)

框架在controller的基類中定義了render方法來(lái)渲染頁(yè)面,它會(huì)調(diào)用類VIEW的靜態(tài)函數(shù)來(lái)分析加載對(duì)應(yīng)頁(yè)面的模板。

<code>public static function display($data, $view_file) {

    if(is_array($data)) {
        extract($data);//extract函數(shù)解析$data數(shù)組中的變量
    }else {
        //拋出變量類型異常
    }

    ob_start();
    ob_implicit_flush(0);
    include self::checkTemplate($view_file);//自定義checkTemplate函數(shù),分析檢查對(duì)應(yīng)的函數(shù)模板,正常返回路徑
    $content = ob_get_clean();

    echo $content;
}
</code>

這里重點(diǎn)說(shuō)一下ob(output buffering)系列函數(shù),其作用引用簡(jiǎn)明代魔法的ob作用介紹:

  • 防止在瀏覽器有輸出之后再使用setcookie,或者h(yuǎn)eader,session_start函數(shù)造成的錯(cuò)誤。其實(shí)這樣的用法少用為好,養(yǎng)成良好的代碼習(xí)慣。
  • 捕捉對(duì)一些不可獲取的函數(shù)的輸出,比如phpinfo會(huì)輸出一大堆的HTML,但是我們無(wú)法用一個(gè)變量例如$info=phpinfo();來(lái)捕捉,這時(shí)候ob就管用了。
  • 對(duì)輸出的內(nèi)容進(jìn)行處理,例如進(jìn)行g(shù)zip壓縮,例如進(jìn)行簡(jiǎn)繁轉(zhuǎn)換,例如進(jìn)行一些字符串替換。
  • 生成靜態(tài)文件,其實(shí)就是捕捉整頁(yè)的輸出,然后存成文件,經(jīng)常在生成HTML,或者整頁(yè)緩存中使用。

它在ob_start()函數(shù)執(zhí)行后,打開(kāi)緩沖區(qū),將后面的輸出內(nèi)容裝進(jìn)系統(tǒng)的緩沖區(qū),ob_implicit_flush(0)函數(shù)來(lái)關(guān)閉絕對(duì)刷送(echo等),最后使用ob_get_clean()函數(shù)將緩沖區(qū)的內(nèi)容取出來(lái)。


類__URL__常量和全局類

TP里的__URL__等全局常量用著很方便,可以很簡(jiǎn)單的實(shí)現(xiàn)跳轉(zhuǎn)等操作,而定義它的函數(shù)createUrl函數(shù)我又想重用,于是借鑒YII的全局類定義方法:

定義基類及詳細(xì)方法(以后的全局方法會(huì)寫(xiě)在這里)

<code>class BaseSqier{
    //方法根據(jù)傳入的$info信息,和當(dāng)前URL_MODE解析返回URL字符串
    public static function createUrl($info = '') {
        $url_info = explode('/', strtolower($info));
        $controller = isset($url_info[1]) ? $url_info[0] : strtolower(CONTROLLER);
        $action = isset($url_info[1]) ? $url_info[1] : $url_info[0];
        switch(URL_MODE){
            case URL_COMMON:
                return "/index.php?r=" . $controller . '/' . $action;
            case URL_REWRITE:
                return '/' .$controller . '/' . $action;
        }
    }
 }
</code>

在啟動(dòng)文件中定義類并繼承基類;

<code>require_once SQ_PATH.'BaseSqier.php';
class SQ extends BaseSqier{
}
</code>

在全局內(nèi)都可以直接使用SQ::createUrl()方法來(lái)創(chuàng)建URL了。這樣,定義__URL__常量就很輕松了。


用單例模式定義數(shù)據(jù)庫(kù)連接基類

<code>class Db {
    protected static $_instance;
    public static function getInstance() {
        if(!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    private function __construct() {
        $link = new \mysqli(DB_HOST, DB_USER, DB_PWD, DB_NAME) or die("連接數(shù)據(jù)庫(kù)失敗,請(qǐng)檢查數(shù)據(jù)庫(kù)配置信息!");
        $link->query('set names utf8');
    }
    public function __clone() {
        return self::getInstance();
    }
}
</code>

使用單例模式的核心是:

  • 私有化構(gòu)造函數(shù),使無(wú)法用new來(lái)創(chuàng)建對(duì)象,也防止子類繼承它并改寫(xiě)其構(gòu)造函數(shù);
  • 用靜態(tài)變量存放當(dāng)前對(duì)象,定義靜態(tài)方法來(lái)返回對(duì)象,如對(duì)象還未實(shí)例化,實(shí)例化一個(gè),存入靜態(tài)變量并返回。
  • 構(gòu)造其__clone魔術(shù)方法,防止clone出一個(gè)新的對(duì)象;

DB類的sql查詢函數(shù)

DB查詢函數(shù)是一個(gè)很復(fù)雜的部分,它是一個(gè)自成體系的東西,像TP和YII的查詢方法都有其獨(dú)特的地方。我這里暫時(shí)先借用TP的MODEL基類,有時(shí)間再慢慢補(bǔ)這個(gè)。

嗯,介紹一下像TP的查詢里的方法聯(lián)查的實(shí)現(xiàn),其訣竅在于,在每個(gè)聯(lián)查方法的最后都用 return this 來(lái)返回已處理過(guò)的查詢對(duì)象。


后續(xù)

yii2里的數(shù)據(jù)表和model類屬性之間的映射很酷(雖然被深坑過(guò)), 前面一直避開(kāi)的模塊(module,我可以想像得到把它也添加到URI時(shí)解析的麻煩)有時(shí)間考慮一下。

邊寫(xiě)邊優(yōu)化。

Well, to be continued... By the way, promote your personal website: www.alwayscoding.cn My contact information is on the right side of the message board page. If you have any questions, you can communicate there.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1116381.htmlTechArticleExperiences in building your own PHP framework (2), Continuation of experience in building PHP frameworks For this update, I want to say : This framework is perfected by myself, and I am not a PHP master yet, so...
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use JavaScript to determine whether two arrays are equal? How to use JavaScript to determine whether two arrays are equal? May 23, 2025 pm 10:51 PM

In JavaScript, you need to use a custom function to determine whether two arrays are equal, because there is no built-in method. 1) Basic implementation is to compare lengths and elements, but cannot process objects and arrays. 2) Recursive depth comparison can handle nested structures, but requires special treatment of NaN. 3) Special types such as functions and dates need to be considered, and further optimization and testing are required.

How to verify social security number string in PHP? How to verify social security number string in PHP? May 23, 2025 pm 08:21 PM

Social security number verification is implemented in PHP through regular expressions and simple logic. 1) Use regular expressions to clean the input and remove non-numeric characters. 2) Check whether the string length is 18 bits. 3) Calculate and verify the check bit to ensure that it matches the last bit of the input.

How to correctly handle this pointing in a closure? How to correctly handle this pointing in a closure? May 21, 2025 pm 09:15 PM

The methods to correctly handle this pointing in JavaScript closures include: 1. Use arrow functions, 2. Use bind methods, 3. Use variables to save this. These methods ensure that this intrinsic function correctly points to the context of the external function.

How to implement data encryption with JavaScript? How to implement data encryption with JavaScript? May 23, 2025 pm 11:12 PM

Using JavaScript to implement data encryption can use the Crypto-JS library. 1. Install and introduce the Crypto-JS library. 2. Use the AES algorithm for encryption and decryption to ensure that the same key is used. 3. Pay attention to the secure storage and transmission of keys. It is recommended to use CBC mode and environment variables to store keys. 4. Consider using WebWorkers when you need high performance. 5. When processing non-ASCII characters, you need to specify the encoding method.

How to define constructors in PHP? How to define constructors in PHP? May 23, 2025 pm 08:27 PM

In PHP, the constructor is defined by the \_\_construct magic method. 1) Define the \_\_construct method in the class, which will be automatically called when the object is instantiated and is used to initialize the object properties. 2) The constructor can accept any number of parameters and flexibly initialize the object. 3) When defining a constructor in a subclass, you need to call parent::\_\_construct() to ensure that the parent class constructor executes. 4) Through optional parameters and conditions judgment, the constructor can simulate the overload effect. 5) The constructor should be concise and only necessary initialization should be done to avoid complex logic or I/O operations.

Detailed steps to deploy a Joomla website on PhpStudy Detailed steps to deploy a Joomla website on PhpStudy May 16, 2025 pm 08:00 PM

The steps to deploy a Joomla website on PhpStudy include: 1) Configure PhpStudy, ensure that Apache and MySQL services run and check PHP version compatibility; 2) Download and decompress PhpStudy's website from the official Joomla website, and then complete the installation through the browser according to the installation wizard; 3) Make basic configurations, such as setting the website name and adding content.

PHP Dependency Injection: Benefits and Examples PHP Dependency Injection: Benefits and Examples May 17, 2025 am 12:14 AM

The benefits of using dependency injection (DI) in PHP include: 1. Decoupling, making the code more modular; 2. Improve testability and easy to use Mocks or Stubs; 3. Increase flexibility and facilitate reusing of dependencies; 4. Improve reusability, and the classes can be used in different environments. By passing dependencies externally to objects, DI makes the code easier to maintain and extend.

PHP Email Tutorial: Sending Emails Made Easy PHP Email Tutorial: Sending Emails Made Easy May 19, 2025 am 12:10 AM

SendingemailswithPHPisstraightforwardusingthemail()functionormoreadvancedlibrarieslikePHPMailer.1)Usemail()forbasicemails,settingrecipients,subjects,messages,andheaders.2)ForHTMLemails,adjustheaderstospecifyHTMLcontent.3)EmployPHPMailerforenhancedfea

See all articles