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

Home php教程 php手冊 PHP5中MVC結構學習

PHP5中MVC結構學習

Jun 21, 2016 am 09:14 AM
example php quot string

php5

一.介紹

  現(xiàn)在在開發(fā)WEB應用的時候,比較流行的一種做法就是使用“MVC”結構,使用如此方式去開發(fā)WEB應用程序,邏輯性強、簡浩明了,使程序設計起來更加方便,快捷。何為“MVC”呢?簡單的來說,它就是“模型(Model)”、“視圖(View)”及“控制器(Controller)”的結合體,也就是所有的“三層”抽象結構,當然這里所說的“MVC”是針對WEB上應用而言的,“使代碼和頁面設計分開”便是其主導思想,這一思想在使用Java Servlet/JavaServer Pages技術的“Struts”中表現(xiàn)的淋漓盡致,有興趣的可以去http://jakarta.apache.org/struts看看,這種設計模式使的程序設計人員可以專注于代碼的設計、編寫及調試,網(wǎng)頁設計人員可以有更多的時間去投入設計而不用理會具體的功能實現(xiàn),這種分工方式完全適應大型項目或企業(yè)級的分布式應用開發(fā)。

  從PHP5的推出可以看到,其中的面向對象功能越來越完善,使用PHP來開發(fā)大型的商業(yè)網(wǎng)站或者分布式企業(yè)應用已經(jīng)成為可能,如果再結合Zend Optimizer,已經(jīng)實現(xiàn)了代碼的封裝性。

  如何在PHP中使用“MVC”設計模式去開發(fā)WEB應用呢?記住一點(代碼和頁面設計分開),用一個簡單的例子演示一下,比如要從數(shù)據(jù)庫中查詢出會員的資料以顯示在網(wǎng)頁上,這里就需要考慮到兩點:1.連接數(shù)據(jù)庫并取出會員資料,2.把會員資料顯示在網(wǎng)頁上,連接數(shù)據(jù)庫我們使用一個數(shù)據(jù)庫的類,把它叫做“DB”類吧,這個類此時就扮演了“模型(Model)”的角色,接著我們需要編寫一個操作“DB”類的程序以取出數(shù)據(jù),這個程序所扮演的角色就是“控制器(Controller)”,它接受客戶端“POST”或“PUT”的數(shù)據(jù),然后再調用“DB”類以取出數(shù)據(jù),把這些數(shù)據(jù)都存放在“控制器(Controller)”中,最后把數(shù)據(jù)傳遞給“視圖(View)”并按照一定的排版格式顯示出來,從上面的分析可以看出,模板在這里就是扮演了“視圖(View)”的角色,當然僅僅一個模板類是不能說成是MVC的,真正的MVC不是這么簡單的,具體可以參考一下“JSF”。

  “3t”是一個模板類,主要是讀取“控制器(Controller)”的數(shù)據(jù)并進行一些特殊處理,最后通過一些簡單的模板語法把數(shù)據(jù)顯示出來,它具有一些什么樣的特點呢?

解析速度快,可以根據(jù)需要選擇使用html方式緩存或php方式緩存,當然你也可以不用緩存,同樣可以實現(xiàn)快速穩(wěn)定的WEB應用

使用簡單、安裝操作方便,在數(shù)據(jù)的讀取方面類似著名模板類“SMARTY”,在數(shù)據(jù)顯示方面即類似“PHP的語法”又類似“JavaBeans”

可擴展性好,你可以根據(jù)需要隨時加入你想要的功能,因為它是開源的,在不久的日子里,將支持插件功能

伸縮性好,支持最新的PHP5,只要你的PHP版本>=4.0.6即可使用,當然你需要有在服務器操作文件的權限

功能強大,支持模板的多級嵌套,數(shù)組多級循環(huán)等等

當然此模板還需完善的地方很多,有待在各種環(huán)境測試使用才能不斷完善,目前僅在LINUX和WINDOWS環(huán)境下測試通過.

二.安裝

1.解壓后應該可以看到如下目錄結構:

./3tx.x/cmp/ 編譯后的文件(請確保此文件夾可讀寫)
./3tx.x/tpl/ 模板文件(模板文件都放在這里,確保此文件夾可讀)
./3tx.x/che/ 緩存文件存放的文件夾(請確保此文件夾可讀寫)
./3tx.x/ttt/ttt.php 3T模板類文件
./3tx.x/ 程序文件(您寫的程序都放在這里)

2.您的PHP版本不能低于PHP4.0.6,我建議你的PHP版本升級至4.3.0以上,程序的整體性能將得到會大幅度提高

3.運行時如出現(xiàn)變量未定義請在程序前加上"error_reporting(7);"函數(shù)

三.語法

模板簡單語法說明:
一般使用左邊大括號"{"和右邊大括號"}"作為模板語法的開始和結束,當然也可以使用自定義的分隔符,如使用"["和"]",以下說明以大括號為分隔符進行說明

(注;下面的[tplCode]和[/tplCode]中間的代碼才是模板語法代碼)

1.在模板文件中使用PHP代碼,如:
[tplCode]
{php}
$i = 3;
echo $i;
{/php}
[/tplCode]
請參考"example6"

2.在模板中使用foreach循環(huán),如:


第一種用法(循環(huán)數(shù)組$a,相當于PHP中的foreach($a as $k=>$v)....)
[/tplCode]
{foreach:$a,$k,$v}
$v = {$v}

{/foreach}
[/tplCode]

第二種用法(可以設定循環(huán)幾次,假如數(shù)組$a有15個元素,則下面的循環(huán)則只取最前面5個)
[tplCode]
{foreach:$a,$k,$v,5}
$v = {$v}

{/foreach}
[/tplCode]

第三種用法(可以設定循環(huán)幾次,假如數(shù)組$a有15個元素,則下面的循環(huán)則從第3個元素開始,取到第5個元素結束)
[tplCode]
{foreach:$a,$k,$v,3,5}
$v = {$v}

{/foreach}
[/tplCode]
請參考"example1"和"example3",在"foreach"循環(huán)中可以使用多維數(shù)組,具體請看"example10"

3.在模板中使用IF語句,如:

第一種用法
[tplCode]
{if:$a == "hello"}
變量$a的值是"hello"
{/if}
[/tplCode]

第二種用法
[tplCode]
{if:$a == true}
變量$a為真
{else}
變量$a不為真
{/if}
[/tplCode]

第三種用法
[tplCode]
{if:$a == 2}
變量$a的值為2
{elseif:$a == 3}
變量$a的值為3
{/if}
[/tplCode]
具體使用請參考"example2"及"example6"

4.在模板中包含模板文件,如:
{tplCode}
{includetpl:head.tpl}
{/tplCode}
這里包含了模板文件"head.tpl",所包含的模板文件必須在同一個目錄下

5.在模板中包含PHP文件,如:
{tplCode}
{includephp:head.php}
{/tplCode}
這里包含了PHP文件"head.php",文件"head.php"在當前程序目錄下
包含文件請看"example8"

6.在模板中輸出時間,如:
{tplCode}
{date:Y-m-d H:i:s}
{/tplCode}
后面的"Y-m-d H:i:s"字符串是標準的PHP時間標識,具體使用可以參考PHP手冊
具體使用請參考"example7"

7.在模板中使用數(shù)學函數(shù)

第一種用法,直接輸出結果
{tplCode}
{math:3*2-5}
{/tplCode}

第二種用法,賦值給指定變量
{tplCode}
{math:3*2-5,$result}
{/tplCode}

第三種用法,賦值給指定變量,第三個參數(shù)設置是否立即輸出,設為"Y"輸出,"N"不輸出
{tplCode}
{math:3*2-5,$result,Y}
{/tplCode}
具體使用請參考"example4"

8.在模板中使用FOR循環(huán)
如下代碼所示
[tplCode]
{for:5,1000,1,$i}
{$i}

{/for}
{/tplCode}
參數(shù)說明:
5:表示從5開始循環(huán)
1000:表示循環(huán)到1000結束
1:表示每次循環(huán)的增量為1,相當于$n++
$i:表示得到每次循環(huán)的值
(上面的"5","1000","1"之類的常數(shù)也可用變量來代替,如:{for:$num,$max,$step,$i},其中的變量是在程序中用"assign()"方法賦值的)
也參考如下代碼(理解一下):
[tplCode]
{for:500,30,-2,$i}
從500開始循環(huán),每次減2,直到30才結束,當前循環(huán)的值是:{$i}

{/for}
{/tplCode}
具體使用請參考"example2","example11"

9.在模板中使用Email標簽
第一種用法:
[tplCode]
{email:redhat@hnwj.net}
[/tplCode]
第二種用法:
[tplCode]
{email:redhat@hnwj.net,Redhat的郵箱}
[/tplCode]
第三種用法:
[tplCode]
{email:redhat@hnwj.net,這是"Redhat"的郵箱這個是帶樣式的class=m,m}
[/tplCode]
具體使用請參考"example5"

10.在模板中定義變量
[tplCode]
{assign:$tplVar,這是我定義的變量可以在模板中輸出也可用PHP代碼輸出}
[/tplCode]
具體使用請參考"example6".

11.其它語法及功能尚在開發(fā)中......
有好的意見或想法請去http://2002.buyionline.net/2002/gbook.php提一下吧,發(fā)現(xiàn)BUG也請及時留言說明一下,謝謝!



注:
1.本模板支持多層嵌套的模板或PHP文件,支持多層foreach或for循環(huán)
2.實際使用技巧
在實際使用過程中如果把屬性$cmpCheck設置為true則每次運行都會編譯PHP程序,否則程序會根據(jù)編譯后的PHP文件的存在時間長短判斷是否要重新編譯
該屬性默值即為true,一般在正在使用中才設為false(可加快速度)
設置方法如:$tttObj->setCmpCheck(true);
3.本程序最大的缺點就是不能準確的捕捉程序中出現(xiàn)的語法錯誤信息
4.暫不支持緩存功能,如果你有好的想法不妨告訴我:-)
5.由于采用的是混編模式編譯模板為PHP文件,所以請不要輸錯了(當然模板是支持大小寫一致的寫法的,也就是說你寫一個{math:38*7}和{MatH:38*7}的效果是一樣的),如輸入"{foreach:$data,k,$v}"編譯將通過,但運行的時候會導致一個語法錯誤,因為里面的"k"前面少了一個"$"符號.本來已經(jīng)寫好了對每一行進行語法分析捕捉錯誤的代碼,但發(fā)現(xiàn)代碼達到幾百行的時候占用時間比較長,如果代碼比較少還可以,但如果較多的話會導致性能的下降.而且PHP本身就有很不錯的出錯信息提示,后來想想就沒有去進行每行代碼的分析了.
6.不知大家是否注意到,在以上的標識中,參數(shù)都是不帶引號或雙引號的(條件判斷語句除外),希望注意哦:-)

四.使用

1.建立PHP文件(命名為first.php,保存在當前目錄下,即"./"),內容如下:
require_once "./ttt/ttt.php";//引入類文件
$ttt = new TTT();//初始化3T模板類的實例
$ttt->setTplDir("./tpl/");//需要編譯的模板文件存放目錄
$ttt->setCmpDir("./cmp/");//編譯后文件的存放目錄
$ttt->assign('title','天空的顏色');//設置變量
$ttt->assign('content','藍色,天氣不錯,萬里無云,晴');//設置變量
$ttt->assign('foot','歡迎歡迎');//設置變量
$ttt->display('first.tpl');//輸出
?>

2.建立tpl文件(命名為"first.tpl",保存在目錄"./tpl/"下).內容如下:





{$title}


{$content}



{$foot}



3.
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)

Hot Topics

PHP Tutorial
1488
72
PHP calls AI intelligent voice assistant PHP voice interaction system construction PHP calls AI intelligent voice assistant PHP voice interaction system construction Jul 25, 2025 pm 08:45 PM

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

How to use PHP to build social sharing functions PHP sharing interface integration practice How to use PHP to build social sharing functions PHP sharing interface integration practice Jul 25, 2025 pm 08:51 PM

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

How to use PHP combined with AI to achieve text error correction PHP syntax detection and optimization How to use PHP combined with AI to achieve text error correction PHP syntax detection and optimization Jul 25, 2025 pm 08:57 PM

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy Jul 25, 2025 pm 08:27 PM

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

How to use PHP to combine AI to generate image. PHP automatically generates art works How to use PHP to combine AI to generate image. PHP automatically generates art works Jul 25, 2025 pm 07:21 PM

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP realizes commodity inventory management and monetization PHP inventory synchronization and alarm mechanism PHP realizes commodity inventory management and monetization PHP inventory synchronization and alarm mechanism Jul 25, 2025 pm 08:30 PM

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Jul 27, 2025 am 04:31 AM

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

PHP integrated AI speech recognition and translator PHP meeting record automatic generation solution PHP integrated AI speech recognition and translator PHP meeting record automatic generation solution Jul 25, 2025 pm 07:06 PM

Select the appropriate AI voice recognition service and integrate PHPSDK; 2. Use PHP to call ffmpeg to convert recordings into API-required formats (such as wav); 3. Upload files to cloud storage and call API asynchronous recognition; 4. Analyze JSON results and organize text using NLP technology; 5. Generate Word or Markdown documents to complete the automation of meeting records. The entire process needs to ensure data encryption, access control and compliance to ensure privacy and security.

See all articles