http://www.phpe.net/articles/382.shtml
MVC模式在网站架构中十分常见。它允许我们建立一个三层结构的应用程式,从代码中分离出有用的层,帮助设计师和开发者协同工作 以及提高我们维护和扩展既有程式的能力。
视图(View)
“视图”主要指我们送到Web浏览器的最终结果??比如我们的脚本生成的HTML。当说到 视图时,很多人想到的是模版,但是把模板方案叫做视图的正确性是值得怀疑的。
对视图来说,最重要的事情可能是它应该是“自我意识 (self aware)”的,视图被渲染(render)时,视图的元素能意识到自己在更大框架中的角色。
以XML为例,可以说XML 在被解析时,DOM API有着这样的认知??一个DOM树里的节点知道它在哪里和它包含了什么。 (当一个XML文档中的节点用SAX解析时只有当解析到该节点时它才有意义。)
绝大多数模板方案使用简单的过程语言和这样的模板标签:
<p>{some_text}</p> <p>{some_more_text}</p>
它们在文档中没有意义,它们代表的意义只是PHP将用其他的东西来替换它。
如果你同意这种对视图的松散描述,你也 就会同意绝大多数模板方案并没有有效的分离视图和模型。模板标签将被替换成什么存放在模型中。
在你实现视图时问自己几个问题:“全体视图 的替换容易吗?”“实现一个新视图要多久?” “能很容易的替换视图的描述语言吗?(比如在同一个视图中用SOAP文档替换HTML文档)”
模 型(Model)
模型代表了程序逻辑。(在企业级程序中经常称为业务层(business layer))
总 的来说,模型的任务是把原有数据转换成包含某些意义的数据,这些数据将被视图所显示。通常,模型将封装数据查询,可能通过一些抽象数据类(数据访问层)来 实现查询。举例说,你希望计算英国年度降雨量(只是为了给你自己找个好点的度假地),模型将接收十年中每天的降雨量,计算出平均值,再传递给视图。
控 制器(controller)
简单的说控制器是Web应用中进入的HTTP请求最先调用的一部分。它检查收到的请求,比如一些 GET变量,做出合适的反馈。在写出你的第一个控制器之前,你很难开始编写其他的PHP代码。最常见的用法是index.php中像switch语句的结 构:
<p> </p> <p class="sycode"> 代码 <p class="sycode"> <p class="sycode"> 1 <? php 2 switch ( $_GET [ ' viewpage ' ]) { 3 case " news " : 4 $page = new NewsRenderer; 5 break ; 6 case " links " : 7 $page = new LinksRenderer; 8 break ; 9 default : 10 $page = new HomePageRenderer; 11 break ; } 12 $page -> display(); ?> </p> </p> </p><p> </p>这段代码混用了面向过程和对象的代码,但是对于小的站点来说,这通常是最好的选择。虽然上边的代码还可以优化。控 制器实际上是用来触发模型的数据和视图元素之间的绑定的控件。例子这里是一个使用MVC模式的简 单例子。首先我们需要一个数据库访问类,它是一个普通类。
在它上边放上模型。
代码
1 php
2 /* *
3 * A simple class for querying MySQL */
4 class DataAccess {
5 /* *
6 * Private
7 * $db stores a database resource
8 */
9 var $db ;
10 /* *
11 * Private
12 * $query stores a query resource
13 */
14 var $query ;
15 // Query resource
16 //! A constructor.
17 /* *
18 * Constucts a new DataAccess object
19 * @param $host string hostname for dbserver
20 * @param $user string dbserver user
21 * @param $pass string dbserver user password
22 * @param $db string database name
23 */
24 function DataAccess ( $host , $user , $pass , $db ) {
25 $this -> db = mysql_pconnect ( $host , $user , $pass );
26 mysql_select_db ( $db , $this -> db);
27 }
28 // ! An accessor
29 /* *
30 * Fetches a query resources and stores it in a local member
31 * @param $sql string the database query to run
32 * @return void
33 */
34 function fetch( $sql ) {
35 $this -> query = mysql_unbuffered_query ( $sql , $this -> db); // Perform query here
36 }
37 // ! An accessor
38 /* *
39 * Returns an associative array of a query row
40 * @return mixed
41 */
42 function getRow () {
43 if ( $row = mysql_fetch_array ( $this -> query , MYSQL_ASSOC) )
44 return $row ;
45 else
46 return false ;
47 }
48 }
49 ?>
50
有一点要注意的是,在模型和数据访问类之间,它们的交互从不会多于一行??没有多行被传送,那样会很快使程式慢下来。同样的程式对 于使用模式的类,它只需要在内存中保留一行(Row)??其他的交给已保存的查询资源(query resource)??换句话说,我们让MYSQL替我们保持结果。
接下来是视图??我去掉了HTML以节省空间,你可以查看这篇文章的 完整代码。
代码
php
/* *
* Binds product data to HTML rendering */
class ProductView {
/* *
* Private
* $model an instance of the ProductModel class
*/
var $model ;
/* *
* Private
* $output rendered HTML is stored here for display
*/
var $output ;
// ! A constructor.
/* *
* Constucts a new ProductView object
* @param $model an instance of the ProductModel class
*/
function ProductView ( & $model ) {
$this -> model =& $model ;
}
// ! A manipulator
/* *
* Builds the top of an HTML page
* @return void
*/
function header () {
}
// ! A manipulator
/* *
* Builds the bottom of an HTML page
* @return void
*/
function footer () {
}
// ! A manipulator
/* *
* Displays a single product
* @return void
*/
function productItem( $id = 1 ) {
$this -> model -> listProduct( $id );
while ( $product = $this -> model -> getProduct() ) {
// Bind data to HTML
}
}
// ! A manipulator
/* *
* Builds a product table
* @return void
*/
function productTable( $rownum = 1 ) {
$rowsperpage = ' 20 ' ;
$this -> model -> listProducts( $rownum , $rowsperpage );
while ( $product = $this -> model -> getProduct() ) {
// Bind data to HTML
}
} // ! An accessor
/* *
* Returns the rendered HTML
* @return string */
function display () {
return $this -> output;
}
}
?>
最后是控制器,我们将把视图实现为一个子类。
代码
1 php
2 /* *
3 * Controls the application
4 */
5 class ProductController extends ProductView {
6 // ! A constructor.
7 /* *
8 * Constucts a new ProductController object
9 * @param $model an instance of the ProductModel class
10 * @param $getvars the incoming HTTP GET method variables
11 */
12 function ProductController ( & $model , $getvars = null ) {
13 ProductView :: ProductView( $model );
14 $this -> header ();
15 switch ( $getvars [ ' view ' ] ) {
16 case " product " :
17 $this -> productItem( $getvars [ ' id ' ]);
18 break ;
19 default :
20 if ( empty ( $getvars [ ' rownum ' ]) ) {
21 $this -> productTable();
22 } else {
23 $this -> productTable( $getvars [ ' rownum ' ]);
24 } break ;
25 }
26 $this -> footer();
27 }
28 }
29 ?>
30
注意这不是实现MVC的唯一方式??比如你可以用控制器实现模型同时整合视图。这只是演示模式 的一种方法。
我们的index.php 文件看起来像这样:
代码
1 php
2 require_once ( ' lib/DataAccess.php ' );
3 require_once ( ' lib/ProductModel.php ' );
4 require_once ( ' lib/ProductView.php ' );
5 require_once ( ' lib/ProductController.php ' );
6 $dao =& new DataAccess ( ' localhost ' , ' user ' , ' pass ' , ' dbname ' );
7 $productModel =& new ProductModel( $dao );
8 $productController =& new ProductController( $productModel , $_GET );
9 echo $productController -> display();
10 ?>
11
漂亮而简单。
我们有一些使用控制器的技巧,在PHP中你可以这样做:
<p class="sycode"> <p class="sycode"> 1 $this -> { $_GET [ ' method ' ]}( $_GET [ ' param ' ]); </p> </p><p> </p>
一个建议是你最好定义程序URL的名字空间形式(namespace),那样它会比较规范比如:
<p class="sycode"> <p class="sycode"> 1 " index.php?class=ProductView&method=productItem&id=4 " </p> </p><p> </p>
通过它我们可以这样处理我们的控制器:
<p class="sycode"> <p class="sycode"> 1 $view = new $_GET [ ' class ' ]; 2 $view -> { $_GET [ ' method ' ]( $_GET [ ' id ' ]); </p> </p><p> </p>
有时候,建立控制器是件很困难的事情,比如当你在开发速度和适应性之间权衡时。一个获得灵感的好去处是Apache group 的Java Struts,它的控制器完全是由XML文档定义的。

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

PHP在現代化進程中仍然重要,因為它支持大量網站和應用,並通過框架適應開發需求。 1.PHP7提升了性能並引入了新功能。 2.現代框架如Laravel、Symfony和CodeIgniter簡化開發,提高代碼質量。 3.性能優化和最佳實踐進一步提升應用效率。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

PHP中使用clone關鍵字創建對象副本,並通過\_\_clone魔法方法定制克隆行為。 1.使用clone關鍵字進行淺拷貝,克隆對象的屬性但不克隆對象屬性內的對象。 2.通過\_\_clone方法可以深拷貝嵌套對象,避免淺拷貝問題。 3.注意避免克隆中的循環引用和性能問題,優化克隆操作以提高效率。

PHP適用於Web開發和內容管理系統,Python適合數據科學、機器學習和自動化腳本。 1.PHP在構建快速、可擴展的網站和應用程序方面表現出色,常用於WordPress等CMS。 2.Python在數據科學和機器學習領域表現卓越,擁有豐富的庫如NumPy和TensorFlow。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境