


Experience in building your own PHP framework (2), experience in building PHP framework_PHP tutorial
Jul 12, 2016 am 08:55 AM搭建自己的PHP框架心得(二),搭建php框架心得
續(xù)言
對(duì)于本次更新,我想說(shuō):
- 本框架由本人挑時(shí)間完善,而我還不是PHP大神級(jí)的人物,所以框架漏洞難免,求大神們指出。
- 本框架的知識(shí)點(diǎn)應(yīng)用都會(huì)寫在博客里,大家有什么異議的可以一起討論,也希望看博客的也能學(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í)行后,打開緩沖區(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ì)寫在這里)
<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ì)象,也防止子類繼承它并改寫其構(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ò)), 前面一直避開的模塊(module,我可以想像得到把它也添加到URI時(shí)解析的麻煩)有時(shí)間考慮一下。
邊寫邊優(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.

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

To merge two PHP arrays and keep unique values, there are two main methods. 1. For index arrays or only deduplication, use array_merge and array_unique combinations: first merge array_merge($array1,$array2) and then use array_unique() to deduplicate them to finally get a new array containing all unique values; 2. For associative arrays and want to retain key-value pairs in the first array, use the operator: $result=$array1 $array2, which will ensure that the keys in the first array will not be overwritten by the second array. These two methods are applicable to different scenarios, depending on whether the key name is retained or only the focus is on

exit() is a function in PHP that is used to terminate script execution immediately. Common uses include: 1. Terminate the script in advance when an exception is detected, such as the file does not exist or verification fails; 2. Output intermediate results during debugging and stop execution; 3. Call exit() after redirecting in conjunction with header() to prevent subsequent code execution; In addition, exit() can accept string parameters as output content or integers as status code, and its alias is die().

The rational use of semantic tags in HTML can improve page structure clarity, accessibility and SEO effects. 1. Used for independent content blocks, such as blog posts or comments, it must be self-contained; 2. Used for classification related content, usually including titles, and is suitable for different modules of the page; 3. Used for auxiliary information related to the main content but not core, such as sidebar recommendations or author profiles. In actual development, labels should be combined and other, avoid excessive nesting, keep the structure simple, and verify the rationality of the structure through developer tools.

There are two ways to create an array in PHP: use the array() function or use brackets []. 1. Using the array() function is a traditional way, with good compatibility. Define index arrays such as $fruits=array("apple","banana","orange"), and associative arrays such as $user=array("name"=>"John","age"=>25); 2. Using [] is a simpler way to support since PHP5.4, such as $color

When you encounter the prompt "This operation requires escalation of permissions", it means that you need administrator permissions to continue. Solutions include: 1. Right-click the "Run as Administrator" program or set the shortcut to always run as an administrator; 2. Check whether the current account is an administrator account, if not, switch or request administrator assistance; 3. Use administrator permissions to open a command prompt or PowerShell to execute relevant commands; 4. Bypass the restrictions by obtaining file ownership or modifying the registry when necessary, but such operations need to be cautious and fully understand the risks. Confirm permission identity and try the above methods usually solve the problem.

The way to process raw POST data in PHP is to use $rawData=file_get_contents('php://input'), which is suitable for receiving JSON, XML, or other custom format data. 1.php://input is a read-only stream, which is only valid in POST requests; 2. Common problems include server configuration or middleware reading input streams, which makes it impossible to obtain data; 3. Application scenarios include receiving front-end fetch requests, third-party service callbacks, and building RESTfulAPIs; 4. The difference from $_POST is that $_POST automatically parses standard form data, while the original data is suitable for non-standard formats and allows manual parsing; 5. Ordinary HTM

To safely handle PHP file uploads, you need to verify the source and type, control the file name and path, set server restrictions, and process media files twice. 1. Verify the upload source to prevent CSRF through token and detect the real MIME type through finfo_file using whitelist control; 2. Rename the file to a random string and determine the extension to store it in a non-Web directory according to the detection type; 3. PHP configuration limits the upload size and temporary directory Nginx/Apache prohibits access to the upload directory; 4. The GD library resaves the pictures to clear potential malicious data.

InPHP,variablesarepassedbyvaluebydefault,meaningfunctionsorassignmentsreceiveacopyofthedata,whilepassingbyreferenceallowsmodificationstoaffecttheoriginalvariable.1.Whenpassingbyvalue,changestothecopydonotimpacttheoriginal,asshownwhenassigning$b=$aorp
