MVC模式的PHP实现_PHP
MVC
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语句的结构:
<?php switch (<blockquote>MVC模式在网站架构中十分常见。它允许我们建立一个三层结构的应用程式,从代码中分离出有用的层,帮助设计师和开发者协同工作以及提高我们维护和扩展既有程式的能力。
视图(View)
“视图”主要指我们送到Web浏览器的最终结果——比如我们的脚本生成的HTML。当说到视图时,很多人想到的是模版,但是把模板方案叫做视图的正确性是值得怀疑的。
对视图来说,最重要的事情可能是它应该是“自我意识(self aware)”的,视图被渲染(render)时,视图的元素能意识到自己在更大框架中的角色。
以XML为例,可以说XML在被解析时,DOM API有着这样的认知——一个DOM树里的节点知道它在哪里和它包含了什么。 (当一个XML文档中的节点用SAX解析时只有当解析到该节点时它才有意义。)
绝大多数模板方案使用简单的过程语言和这样的模板标签:
<p>{some_text}</p> <p>{some_more_text}</p>Copy after login
它们在文档中没有意义,它们代表的意义只是PHP将用其他的东西来替换它。
如果你同意这种对视图的松散描述,你也就会同意绝大多数模板方案并没有有效的分离视图和模型。模板标签将被替换成什么存放在模型中。
在你实现视图时问自己几个问题:“全体视图的替换容易吗?”“实现一个新视图要多久?” “能很容易的替换视图的描述语言吗?(比如在同一个视图中用SOAP文档替换HTML文档)”
模型(Model)
模型代表了程序逻辑。(在企业级程序中经常称为业务层(business layer))
总的来说,模型的任务是把原有数据转换成包含某些意义的数据,这些数据将被视图所显示。通常,模型将封装数据查询,可能通过一些抽象数据类(数据访问层)来实现查询。举例说,你希望计算英国年度降雨量(只是为了给你自己找个好点的度假地),模型将接收十年中每天的降雨量,计算出平均值,再传递给视图。
控制器(controller)
简单的说控制器是Web应用中进入的HTTP请求最先调用的一部分。它检查收到的请求,比如一些GET变量,做出合适的反馈。在写出你的第一个控制器之前,你很难开始编写其他的PHP代码。最常见的用法是index.php中像switch语句的结构:
___FCKpd___1Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
这段代码混用了面向过程和对象的代码,但是对于小的站点来说,这通常是最好的选择。虽然上边的代码还可以优化。
控制器实际上是用来触发模型的数据和视图元素之间的绑定的控件。
例子
这里是一个使用MVC模式的简单例子。
首先我们需要一个数据库访问类,它是一个普通类。
<?php /** * A simple class for querying MySQL */ class DataAccess { /** * Private * $db stores a database resource */ var $db; /** * Private * $query stores a query resource */ var $query; // Query resource //! A constructor. /** * Constucts a new DataAccess object * @param $host string hostname for dbserver * @param $user string dbserver user * @param $pass string dbserver user password * @param $db string database name */ function DataAccess ($host,$user,$pass,$db) { $this->db=mysql_pconnect($host,$user,$pass); mysql_select_db($db,$this->db); } //! An accessor /** * Fetches a query resources and stores it in a local member * @param $sql string the database query to run * @return void */ function fetch($sql) { $this->query=mysql_unbuffered_query($sql,$this->db); // Perform query here } //! An accessor /** * Returns an associative array of a query row * @return mixed */ function getRow () { if ( $row=mysql_fetch_array($this->query,MYSQL_ASSOC) ) return $row; else return false; } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
在它上边放上模型。
<?php /** * Fetches "products" from the database */ class ProductModel { /** * Private * $dao an instance of the DataAccess class */ var $dao; //! A constructor. /** * Constucts a new ProductModel object * @param $dbobject an instance of the DataAccess class */ function ProductModel (&$dao) { $this->dao=& $dao; } //! A manipulator /** * Tells the $dboject to store this query as a resource * @param $start the row to start from * @param $rows the number of rows to fetch * @return void */ function listProducts($start=1,$rows=50) { $this->dao->fetch("SELECT * FROM products LIMIT ".$start.", ".$rows); } //! A manipulator /** * Tells the $dboject to store this query as a resource * @param $id a primary key for a row * @return void */ function listProduct($id) { $this->dao->fetch("SELECT * FROM products WHERE PRODUCTID='".$id."'"); } //! A manipulator /** * Fetches a product as an associative array from the $dbobject * @return mixed */ function getProduct() { if ( $product=$this->dao->getRow() ) return $product; else return false; } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
有一点要注意的是,在模型和数据访问类之间,它们的交互从不会多于一行——没有多行被传送,那样会很快使程式慢下来。同样的程式对于使用模式的类,它只需要在内存中保留一行(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; } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
最后是控制器,我们将把视图实现为一个子类。
<?php /** * Controls the application */ class ProductController extends ProductView { //! A constructor. /** * Constucts a new ProductController object * @param $model an instance of the ProductModel class * @param $getvars the incoming HTTP GET method variables */ function ProductController (&$model,$getvars=null) { ProductView::ProductView($model); $this->header(); switch ( $getvars['view'] ) { case "product": $this->productItem($getvars['id']); break; default: if ( empty ($getvars['rownum']) ) { $this->productTable(); } else { $this->productTable($getvars['rownum']); } break; } $this->footer(); } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
注意这不是实现MVC的唯一方式——比如你可以用控制器实现模型同时整合视图。这只是演示模式的一种方法。
我们的index.php 文件看起来像这样:
MVC模式在网站架构中十分常见。它允许我们建立一个三层结构的应用程式,从代码中分离出有用的层,帮助设计师和开发者协同工作以及提高我们维护和扩展既有程式的能力。
视图(View)
“视图”主要指我们送到Web浏览器的最终结果——比如我们的脚本生成的HTML。当说到视图时,很多人想到的是模版,但是把模板方案叫做视图的正确性是值得怀疑的。
对视图来说,最重要的事情可能是它应该是“自我意识(self aware)”的,视图被渲染(render)时,视图的元素能意识到自己在更大框架中的角色。
以XML为例,可以说XML在被解析时,DOM API有着这样的认知——一个DOM树里的节点知道它在哪里和它包含了什么。 (当一个XML文档中的节点用SAX解析时只有当解析到该节点时它才有意义。)
绝大多数模板方案使用简单的过程语言和这样的模板标签:
<p>{some_text}</p> <p>{some_more_text}</p>Copy after login
它们在文档中没有意义,它们代表的意义只是PHP将用其他的东西来替换它。
如果你同意这种对视图的松散描述,你也就会同意绝大多数模板方案并没有有效的分离视图和模型。模板标签将被替换成什么存放在模型中。
在你实现视图时问自己几个问题:“全体视图的替换容易吗?”“实现一个新视图要多久?” “能很容易的替换视图的描述语言吗?(比如在同一个视图中用SOAP文档替换HTML文档)”
模型(Model)
模型代表了程序逻辑。(在企业级程序中经常称为业务层(business layer))
总的来说,模型的任务是把原有数据转换成包含某些意义的数据,这些数据将被视图所显示。通常,模型将封装数据查询,可能通过一些抽象数据类(数据访问层)来实现查询。举例说,你希望计算英国年度降雨量(只是为了给你自己找个好点的度假地),模型将接收十年中每天的降雨量,计算出平均值,再传递给视图。
控制器(controller)
简单的说控制器是Web应用中进入的HTTP请求最先调用的一部分。它检查收到的请求,比如一些GET变量,做出合适的反馈。在写出你的第一个控制器之前,你很难开始编写其他的PHP代码。最常见的用法是index.php中像switch语句的结构:
<?php switch (<blockquote>MVC模式在网站架构中十分常见。它允许我们建立一个三层结构的应用程式,从代码中分离出有用的层,帮助设计师和开发者协同工作以及提高我们维护和扩展既有程式的能力。
视图(View)
“视图”主要指我们送到Web浏览器的最终结果——比如我们的脚本生成的HTML。当说到视图时,很多人想到的是模版,但是把模板方案叫做视图的正确性是值得怀疑的。
对视图来说,最重要的事情可能是它应该是“自我意识(self aware)”的,视图被渲染(render)时,视图的元素能意识到自己在更大框架中的角色。
以XML为例,可以说XML在被解析时,DOM API有着这样的认知——一个DOM树里的节点知道它在哪里和它包含了什么。 (当一个XML文档中的节点用SAX解析时只有当解析到该节点时它才有意义。)
绝大多数模板方案使用简单的过程语言和这样的模板标签:
<p>{some_text}</p> <p>{some_more_text}</p>Copy after login
它们在文档中没有意义,它们代表的意义只是PHP将用其他的东西来替换它。
如果你同意这种对视图的松散描述,你也就会同意绝大多数模板方案并没有有效的分离视图和模型。模板标签将被替换成什么存放在模型中。
在你实现视图时问自己几个问题:“全体视图的替换容易吗?”“实现一个新视图要多久?” “能很容易的替换视图的描述语言吗?(比如在同一个视图中用SOAP文档替换HTML文档)”
模型(Model)
模型代表了程序逻辑。(在企业级程序中经常称为业务层(business layer))
总的来说,模型的任务是把原有数据转换成包含某些意义的数据,这些数据将被视图所显示。通常,模型将封装数据查询,可能通过一些抽象数据类(数据访问层)来实现查询。举例说,你希望计算英国年度降雨量(只是为了给你自己找个好点的度假地),模型将接收十年中每天的降雨量,计算出平均值,再传递给视图。
控制器(controller)
简单的说控制器是Web应用中进入的HTTP请求最先调用的一部分。它检查收到的请求,比如一些GET变量,做出合适的反馈。在写出你的第一个控制器之前,你很难开始编写其他的PHP代码。最常见的用法是index.php中像switch语句的结构:
___FCKpd___1Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
这段代码混用了面向过程和对象的代码,但是对于小的站点来说,这通常是最好的选择。虽然上边的代码还可以优化。
控制器实际上是用来触发模型的数据和视图元素之间的绑定的控件。
例子
这里是一个使用MVC模式的简单例子。
首先我们需要一个数据库访问类,它是一个普通类。
<?php /** * A simple class for querying MySQL */ class DataAccess { /** * Private * $db stores a database resource */ var $db; /** * Private * $query stores a query resource */ var $query; // Query resource //! A constructor. /** * Constucts a new DataAccess object * @param $host string hostname for dbserver * @param $user string dbserver user * @param $pass string dbserver user password * @param $db string database name */ function DataAccess ($host,$user,$pass,$db) { $this->db=mysql_pconnect($host,$user,$pass); mysql_select_db($db,$this->db); } //! An accessor /** * Fetches a query resources and stores it in a local member * @param $sql string the database query to run * @return void */ function fetch($sql) { $this->query=mysql_unbuffered_query($sql,$this->db); // Perform query here } //! An accessor /** * Returns an associative array of a query row * @return mixed */ function getRow () { if ( $row=mysql_fetch_array($this->query,MYSQL_ASSOC) ) return $row; else return false; } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
在它上边放上模型。
<?php /** * Fetches "products" from the database */ class ProductModel { /** * Private * $dao an instance of the DataAccess class */ var $dao; //! A constructor. /** * Constucts a new ProductModel object * @param $dbobject an instance of the DataAccess class */ function ProductModel (&$dao) { $this->dao=& $dao; } //! A manipulator /** * Tells the $dboject to store this query as a resource * @param $start the row to start from * @param $rows the number of rows to fetch * @return void */ function listProducts($start=1,$rows=50) { $this->dao->fetch("SELECT * FROM products LIMIT ".$start.", ".$rows); } //! A manipulator /** * Tells the $dboject to store this query as a resource * @param $id a primary key for a row * @return void */ function listProduct($id) { $this->dao->fetch("SELECT * FROM products WHERE PRODUCTID='".$id."'"); } //! A manipulator /** * Fetches a product as an associative array from the $dbobject * @return mixed */ function getProduct() { if ( $product=$this->dao->getRow() ) return $product; else return false; } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
有一点要注意的是,在模型和数据访问类之间,它们的交互从不会多于一行——没有多行被传送,那样会很快使程式慢下来。同样的程式对于使用模式的类,它只需要在内存中保留一行(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; } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
最后是控制器,我们将把视图实现为一个子类。
<?php /** * Controls the application */ class ProductController extends ProductView { //! A constructor. /** * Constucts a new ProductController object * @param $model an instance of the ProductModel class * @param $getvars the incoming HTTP GET method variables */ function ProductController (&$model,$getvars=null) { ProductView::ProductView($model); $this->header(); switch ( $getvars['view'] ) { case "product": $this->productItem($getvars['id']); break; default: if ( empty ($getvars['rownum']) ) { $this->productTable(); } else { $this->productTable($getvars['rownum']); } break; } $this->footer(); } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
注意这不是实现MVC的唯一方式——比如你可以用控制器实现模型同时整合视图。这只是演示模式的一种方法。
我们的index.php 文件看起来像这样:
___FCKpd___6Copy after loginCopy after login
漂亮而简单。
我们有一些使用控制器的技巧,在PHP中你可以这样做:
$this->{MVC模式在网站架构中十分常见。它允许我们建立一个三层结构的应用程式,从代码中分离出有用的层,帮助设计师和开发者协同工作以及提高我们维护和扩展既有程式的能力。
视图(View)
“视图”主要指我们送到Web浏览器的最终结果——比如我们的脚本生成的HTML。当说到视图时,很多人想到的是模版,但是把模板方案叫做视图的正确性是值得怀疑的。
对视图来说,最重要的事情可能是它应该是“自我意识(self aware)”的,视图被渲染(render)时,视图的元素能意识到自己在更大框架中的角色。
以XML为例,可以说XML在被解析时,DOM API有着这样的认知——一个DOM树里的节点知道它在哪里和它包含了什么。 (当一个XML文档中的节点用SAX解析时只有当解析到该节点时它才有意义。)
绝大多数模板方案使用简单的过程语言和这样的模板标签:
<p>{some_text}</p> <p>{some_more_text}</p>Copy after login
它们在文档中没有意义,它们代表的意义只是PHP将用其他的东西来替换它。
如果你同意这种对视图的松散描述,你也就会同意绝大多数模板方案并没有有效的分离视图和模型。模板标签将被替换成什么存放在模型中。
在你实现视图时问自己几个问题:“全体视图的替换容易吗?”“实现一个新视图要多久?” “能很容易的替换视图的描述语言吗?(比如在同一个视图中用SOAP文档替换HTML文档)”
模型(Model)
模型代表了程序逻辑。(在企业级程序中经常称为业务层(business layer))
总的来说,模型的任务是把原有数据转换成包含某些意义的数据,这些数据将被视图所显示。通常,模型将封装数据查询,可能通过一些抽象数据类(数据访问层)来实现查询。举例说,你希望计算英国年度降雨量(只是为了给你自己找个好点的度假地),模型将接收十年中每天的降雨量,计算出平均值,再传递给视图。
控制器(controller)
简单的说控制器是Web应用中进入的HTTP请求最先调用的一部分。它检查收到的请求,比如一些GET变量,做出合适的反馈。在写出你的第一个控制器之前,你很难开始编写其他的PHP代码。最常见的用法是index.php中像switch语句的结构:
<?php switch (<blockquote>MVC模式在网站架构中十分常见。它允许我们建立一个三层结构的应用程式,从代码中分离出有用的层,帮助设计师和开发者协同工作以及提高我们维护和扩展既有程式的能力。
视图(View)
“视图”主要指我们送到Web浏览器的最终结果——比如我们的脚本生成的HTML。当说到视图时,很多人想到的是模版,但是把模板方案叫做视图的正确性是值得怀疑的。
对视图来说,最重要的事情可能是它应该是“自我意识(self aware)”的,视图被渲染(render)时,视图的元素能意识到自己在更大框架中的角色。
以XML为例,可以说XML在被解析时,DOM API有着这样的认知——一个DOM树里的节点知道它在哪里和它包含了什么。 (当一个XML文档中的节点用SAX解析时只有当解析到该节点时它才有意义。)
绝大多数模板方案使用简单的过程语言和这样的模板标签:
<p>{some_text}</p> <p>{some_more_text}</p>Copy after login
它们在文档中没有意义,它们代表的意义只是PHP将用其他的东西来替换它。
如果你同意这种对视图的松散描述,你也就会同意绝大多数模板方案并没有有效的分离视图和模型。模板标签将被替换成什么存放在模型中。
在你实现视图时问自己几个问题:“全体视图的替换容易吗?”“实现一个新视图要多久?” “能很容易的替换视图的描述语言吗?(比如在同一个视图中用SOAP文档替换HTML文档)”
模型(Model)
模型代表了程序逻辑。(在企业级程序中经常称为业务层(business layer))
总的来说,模型的任务是把原有数据转换成包含某些意义的数据,这些数据将被视图所显示。通常,模型将封装数据查询,可能通过一些抽象数据类(数据访问层)来实现查询。举例说,你希望计算英国年度降雨量(只是为了给你自己找个好点的度假地),模型将接收十年中每天的降雨量,计算出平均值,再传递给视图。
控制器(controller)
简单的说控制器是Web应用中进入的HTTP请求最先调用的一部分。它检查收到的请求,比如一些GET变量,做出合适的反馈。在写出你的第一个控制器之前,你很难开始编写其他的PHP代码。最常见的用法是index.php中像switch语句的结构:
___FCKpd___1Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
这段代码混用了面向过程和对象的代码,但是对于小的站点来说,这通常是最好的选择。虽然上边的代码还可以优化。
控制器实际上是用来触发模型的数据和视图元素之间的绑定的控件。
例子
这里是一个使用MVC模式的简单例子。
首先我们需要一个数据库访问类,它是一个普通类。
<?php /** * A simple class for querying MySQL */ class DataAccess { /** * Private * $db stores a database resource */ var $db; /** * Private * $query stores a query resource */ var $query; // Query resource //! A constructor. /** * Constucts a new DataAccess object * @param $host string hostname for dbserver * @param $user string dbserver user * @param $pass string dbserver user password * @param $db string database name */ function DataAccess ($host,$user,$pass,$db) { $this->db=mysql_pconnect($host,$user,$pass); mysql_select_db($db,$this->db); } //! An accessor /** * Fetches a query resources and stores it in a local member * @param $sql string the database query to run * @return void */ function fetch($sql) { $this->query=mysql_unbuffered_query($sql,$this->db); // Perform query here } //! An accessor /** * Returns an associative array of a query row * @return mixed */ function getRow () { if ( $row=mysql_fetch_array($this->query,MYSQL_ASSOC) ) return $row; else return false; } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
在它上边放上模型。
<?php /** * Fetches "products" from the database */ class ProductModel { /** * Private * $dao an instance of the DataAccess class */ var $dao; //! A constructor. /** * Constucts a new ProductModel object * @param $dbobject an instance of the DataAccess class */ function ProductModel (&$dao) { $this->dao=& $dao; } //! A manipulator /** * Tells the $dboject to store this query as a resource * @param $start the row to start from * @param $rows the number of rows to fetch * @return void */ function listProducts($start=1,$rows=50) { $this->dao->fetch("SELECT * FROM products LIMIT ".$start.", ".$rows); } //! A manipulator /** * Tells the $dboject to store this query as a resource * @param $id a primary key for a row * @return void */ function listProduct($id) { $this->dao->fetch("SELECT * FROM products WHERE PRODUCTID='".$id."'"); } //! A manipulator /** * Fetches a product as an associative array from the $dbobject * @return mixed */ function getProduct() { if ( $product=$this->dao->getRow() ) return $product; else return false; } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
有一点要注意的是,在模型和数据访问类之间,它们的交互从不会多于一行——没有多行被传送,那样会很快使程式慢下来。同样的程式对于使用模式的类,它只需要在内存中保留一行(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; } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
最后是控制器,我们将把视图实现为一个子类。
<?php /** * Controls the application */ class ProductController extends ProductView { //! A constructor. /** * Constucts a new ProductController object * @param $model an instance of the ProductModel class * @param $getvars the incoming HTTP GET method variables */ function ProductController (&$model,$getvars=null) { ProductView::ProductView($model); $this->header(); switch ( $getvars['view'] ) { case "product": $this->productItem($getvars['id']); break; default: if ( empty ($getvars['rownum']) ) { $this->productTable(); } else { $this->productTable($getvars['rownum']); } break; } $this->footer(); } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
注意这不是实现MVC的唯一方式——比如你可以用控制器实现模型同时整合视图。这只是演示模式的一种方法。
我们的index.php 文件看起来像这样:
MVC模式在网站架构中十分常见。它允许我们建立一个三层结构的应用程式,从代码中分离出有用的层,帮助设计师和开发者协同工作以及提高我们维护和扩展既有程式的能力。
视图(View)
“视图”主要指我们送到Web浏览器的最终结果——比如我们的脚本生成的HTML。当说到视图时,很多人想到的是模版,但是把模板方案叫做视图的正确性是值得怀疑的。
对视图来说,最重要的事情可能是它应该是“自我意识(self aware)”的,视图被渲染(render)时,视图的元素能意识到自己在更大框架中的角色。
以XML为例,可以说XML在被解析时,DOM API有着这样的认知——一个DOM树里的节点知道它在哪里和它包含了什么。 (当一个XML文档中的节点用SAX解析时只有当解析到该节点时它才有意义。)
绝大多数模板方案使用简单的过程语言和这样的模板标签:
<p>{some_text}</p> <p>{some_more_text}</p>Copy after login
它们在文档中没有意义,它们代表的意义只是PHP将用其他的东西来替换它。
如果你同意这种对视图的松散描述,你也就会同意绝大多数模板方案并没有有效的分离视图和模型。模板标签将被替换成什么存放在模型中。
在你实现视图时问自己几个问题:“全体视图的替换容易吗?”“实现一个新视图要多久?” “能很容易的替换视图的描述语言吗?(比如在同一个视图中用SOAP文档替换HTML文档)”
模型(Model)
模型代表了程序逻辑。(在企业级程序中经常称为业务层(business layer))
总的来说,模型的任务是把原有数据转换成包含某些意义的数据,这些数据将被视图所显示。通常,模型将封装数据查询,可能通过一些抽象数据类(数据访问层)来实现查询。举例说,你希望计算英国年度降雨量(只是为了给你自己找个好点的度假地),模型将接收十年中每天的降雨量,计算出平均值,再传递给视图。
控制器(controller)
简单的说控制器是Web应用中进入的HTTP请求最先调用的一部分。它检查收到的请求,比如一些GET变量,做出合适的反馈。在写出你的第一个控制器之前,你很难开始编写其他的PHP代码。最常见的用法是index.php中像switch语句的结构:
<?php switch (<blockquote>MVC模式在网站架构中十分常见。它允许我们建立一个三层结构的应用程式,从代码中分离出有用的层,帮助设计师和开发者协同工作以及提高我们维护和扩展既有程式的能力。
视图(View)
“视图”主要指我们送到Web浏览器的最终结果——比如我们的脚本生成的HTML。当说到视图时,很多人想到的是模版,但是把模板方案叫做视图的正确性是值得怀疑的。
对视图来说,最重要的事情可能是它应该是“自我意识(self aware)”的,视图被渲染(render)时,视图的元素能意识到自己在更大框架中的角色。
以XML为例,可以说XML在被解析时,DOM API有着这样的认知——一个DOM树里的节点知道它在哪里和它包含了什么。 (当一个XML文档中的节点用SAX解析时只有当解析到该节点时它才有意义。)
绝大多数模板方案使用简单的过程语言和这样的模板标签:
<p>{some_text}</p> <p>{some_more_text}</p>Copy after login
它们在文档中没有意义,它们代表的意义只是PHP将用其他的东西来替换它。
如果你同意这种对视图的松散描述,你也就会同意绝大多数模板方案并没有有效的分离视图和模型。模板标签将被替换成什么存放在模型中。
在你实现视图时问自己几个问题:“全体视图的替换容易吗?”“实现一个新视图要多久?” “能很容易的替换视图的描述语言吗?(比如在同一个视图中用SOAP文档替换HTML文档)”
模型(Model)
模型代表了程序逻辑。(在企业级程序中经常称为业务层(business layer))
总的来说,模型的任务是把原有数据转换成包含某些意义的数据,这些数据将被视图所显示。通常,模型将封装数据查询,可能通过一些抽象数据类(数据访问层)来实现查询。举例说,你希望计算英国年度降雨量(只是为了给你自己找个好点的度假地),模型将接收十年中每天的降雨量,计算出平均值,再传递给视图。
控制器(controller)
简单的说控制器是Web应用中进入的HTTP请求最先调用的一部分。它检查收到的请求,比如一些GET变量,做出合适的反馈。在写出你的第一个控制器之前,你很难开始编写其他的PHP代码。最常见的用法是index.php中像switch语句的结构:
___FCKpd___1Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
这段代码混用了面向过程和对象的代码,但是对于小的站点来说,这通常是最好的选择。虽然上边的代码还可以优化。
控制器实际上是用来触发模型的数据和视图元素之间的绑定的控件。
例子
这里是一个使用MVC模式的简单例子。
首先我们需要一个数据库访问类,它是一个普通类。
<?php /** * A simple class for querying MySQL */ class DataAccess { /** * Private * $db stores a database resource */ var $db; /** * Private * $query stores a query resource */ var $query; // Query resource //! A constructor. /** * Constucts a new DataAccess object * @param $host string hostname for dbserver * @param $user string dbserver user * @param $pass string dbserver user password * @param $db string database name */ function DataAccess ($host,$user,$pass,$db) { $this->db=mysql_pconnect($host,$user,$pass); mysql_select_db($db,$this->db); } //! An accessor /** * Fetches a query resources and stores it in a local member * @param $sql string the database query to run * @return void */ function fetch($sql) { $this->query=mysql_unbuffered_query($sql,$this->db); // Perform query here } //! An accessor /** * Returns an associative array of a query row * @return mixed */ function getRow () { if ( $row=mysql_fetch_array($this->query,MYSQL_ASSOC) ) return $row; else return false; } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
在它上边放上模型。
<?php /** * Fetches "products" from the database */ class ProductModel { /** * Private * $dao an instance of the DataAccess class */ var $dao; //! A constructor. /** * Constucts a new ProductModel object * @param $dbobject an instance of the DataAccess class */ function ProductModel (&$dao) { $this->dao=& $dao; } //! A manipulator /** * Tells the $dboject to store this query as a resource * @param $start the row to start from * @param $rows the number of rows to fetch * @return void */ function listProducts($start=1,$rows=50) { $this->dao->fetch("SELECT * FROM products LIMIT ".$start.", ".$rows); } //! A manipulator /** * Tells the $dboject to store this query as a resource * @param $id a primary key for a row * @return void */ function listProduct($id) { $this->dao->fetch("SELECT * FROM products WHERE PRODUCTID='".$id."'"); } //! A manipulator /** * Fetches a product as an associative array from the $dbobject * @return mixed */ function getProduct() { if ( $product=$this->dao->getRow() ) return $product; else return false; } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
有一点要注意的是,在模型和数据访问类之间,它们的交互从不会多于一行——没有多行被传送,那样会很快使程式慢下来。同样的程式对于使用模式的类,它只需要在内存中保留一行(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; } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
最后是控制器,我们将把视图实现为一个子类。
<?php /** * Controls the application */ class ProductController extends ProductView { //! A constructor. /** * Constucts a new ProductController object * @param $model an instance of the ProductModel class * @param $getvars the incoming HTTP GET method variables */ function ProductController (&$model,$getvars=null) { ProductView::ProductView($model); $this->header(); switch ( $getvars['view'] ) { case "product": $this->productItem($getvars['id']); break; default: if ( empty ($getvars['rownum']) ) { $this->productTable(); } else { $this->productTable($getvars['rownum']); } break; } $this->footer(); } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
注意这不是实现MVC的唯一方式——比如你可以用控制器实现模型同时整合视图。这只是演示模式的一种方法。
我们的index.php 文件看起来像这样:
___FCKpd___6Copy after loginCopy after login
漂亮而简单。
我们有一些使用控制器的技巧,在PHP中你可以这样做:
___FCKpd___7Copy after login
一个建议是你最好定义程序URL的名字空间形式(namespace),那样它会比较规范比如:
"index.php?class=ProductView&method=productItem&id=4"Copy after login
通过它我们可以这样处理我们的控制器:
$view=newMVC模式在网站架构中十分常见。它允许我们建立一个三层结构的应用程式,从代码中分离出有用的层,帮助设计师和开发者协同工作以及提高我们维护和扩展既有程式的能力。
视图(View)
“视图”主要指我们送到Web浏览器的最终结果——比如我们的脚本生成的HTML。当说到视图时,很多人想到的是模版,但是把模板方案叫做视图的正确性是值得怀疑的。
对视图来说,最重要的事情可能是它应该是“自我意识(self aware)”的,视图被渲染(render)时,视图的元素能意识到自己在更大框架中的角色。
以XML为例,可以说XML在被解析时,DOM API有着这样的认知——一个DOM树里的节点知道它在哪里和它包含了什么。 (当一个XML文档中的节点用SAX解析时只有当解析到该节点时它才有意义。)
绝大多数模板方案使用简单的过程语言和这样的模板标签:
<p>{some_text}</p> <p>{some_more_text}</p>Copy after login
它们在文档中没有意义,它们代表的意义只是PHP将用其他的东西来替换它。
如果你同意这种对视图的松散描述,你也就会同意绝大多数模板方案并没有有效的分离视图和模型。模板标签将被替换成什么存放在模型中。
在你实现视图时问自己几个问题:“全体视图的替换容易吗?”“实现一个新视图要多久?” “能很容易的替换视图的描述语言吗?(比如在同一个视图中用SOAP文档替换HTML文档)”
模型(Model)
模型代表了程序逻辑。(在企业级程序中经常称为业务层(business layer))
总的来说,模型的任务是把原有数据转换成包含某些意义的数据,这些数据将被视图所显示。通常,模型将封装数据查询,可能通过一些抽象数据类(数据访问层)来实现查询。举例说,你希望计算英国年度降雨量(只是为了给你自己找个好点的度假地),模型将接收十年中每天的降雨量,计算出平均值,再传递给视图。
控制器(controller)
简单的说控制器是Web应用中进入的HTTP请求最先调用的一部分。它检查收到的请求,比如一些GET变量,做出合适的反馈。在写出你的第一个控制器之前,你很难开始编写其他的PHP代码。最常见的用法是index.php中像switch语句的结构:
<?php switch (<blockquote>MVC模式在网站架构中十分常见。它允许我们建立一个三层结构的应用程式,从代码中分离出有用的层,帮助设计师和开发者协同工作以及提高我们维护和扩展既有程式的能力。
视图(View)
“视图”主要指我们送到Web浏览器的最终结果——比如我们的脚本生成的HTML。当说到视图时,很多人想到的是模版,但是把模板方案叫做视图的正确性是值得怀疑的。
对视图来说,最重要的事情可能是它应该是“自我意识(self aware)”的,视图被渲染(render)时,视图的元素能意识到自己在更大框架中的角色。
以XML为例,可以说XML在被解析时,DOM API有着这样的认知——一个DOM树里的节点知道它在哪里和它包含了什么。 (当一个XML文档中的节点用SAX解析时只有当解析到该节点时它才有意义。)
绝大多数模板方案使用简单的过程语言和这样的模板标签:
<p>{some_text}</p> <p>{some_more_text}</p>Copy after login
它们在文档中没有意义,它们代表的意义只是PHP将用其他的东西来替换它。
如果你同意这种对视图的松散描述,你也就会同意绝大多数模板方案并没有有效的分离视图和模型。模板标签将被替换成什么存放在模型中。
在你实现视图时问自己几个问题:“全体视图的替换容易吗?”“实现一个新视图要多久?” “能很容易的替换视图的描述语言吗?(比如在同一个视图中用SOAP文档替换HTML文档)”
模型(Model)
模型代表了程序逻辑。(在企业级程序中经常称为业务层(business layer))
总的来说,模型的任务是把原有数据转换成包含某些意义的数据,这些数据将被视图所显示。通常,模型将封装数据查询,可能通过一些抽象数据类(数据访问层)来实现查询。举例说,你希望计算英国年度降雨量(只是为了给你自己找个好点的度假地),模型将接收十年中每天的降雨量,计算出平均值,再传递给视图。
控制器(controller)
简单的说控制器是Web应用中进入的HTTP请求最先调用的一部分。它检查收到的请求,比如一些GET变量,做出合适的反馈。在写出你的第一个控制器之前,你很难开始编写其他的PHP代码。最常见的用法是index.php中像switch语句的结构:
___FCKpd___1Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
这段代码混用了面向过程和对象的代码,但是对于小的站点来说,这通常是最好的选择。虽然上边的代码还可以优化。
控制器实际上是用来触发模型的数据和视图元素之间的绑定的控件。
例子
这里是一个使用MVC模式的简单例子。
首先我们需要一个数据库访问类,它是一个普通类。
<?php /** * A simple class for querying MySQL */ class DataAccess { /** * Private * $db stores a database resource */ var $db; /** * Private * $query stores a query resource */ var $query; // Query resource //! A constructor. /** * Constucts a new DataAccess object * @param $host string hostname for dbserver * @param $user string dbserver user * @param $pass string dbserver user password * @param $db string database name */ function DataAccess ($host,$user,$pass,$db) { $this->db=mysql_pconnect($host,$user,$pass); mysql_select_db($db,$this->db); } //! An accessor /** * Fetches a query resources and stores it in a local member * @param $sql string the database query to run * @return void */ function fetch($sql) { $this->query=mysql_unbuffered_query($sql,$this->db); // Perform query here } //! An accessor /** * Returns an associative array of a query row * @return mixed */ function getRow () { if ( $row=mysql_fetch_array($this->query,MYSQL_ASSOC) ) return $row; else return false; } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
在它上边放上模型。
<?php /** * Fetches "products" from the database */ class ProductModel { /** * Private * $dao an instance of the DataAccess class */ var $dao; //! A constructor. /** * Constucts a new ProductModel object * @param $dbobject an instance of the DataAccess class */ function ProductModel (&$dao) { $this->dao=& $dao; } //! A manipulator /** * Tells the $dboject to store this query as a resource * @param $start the row to start from * @param $rows the number of rows to fetch * @return void */ function listProducts($start=1,$rows=50) { $this->dao->fetch("SELECT * FROM products LIMIT ".$start.", ".$rows); } //! A manipulator /** * Tells the $dboject to store this query as a resource * @param $id a primary key for a row * @return void */ function listProduct($id) { $this->dao->fetch("SELECT * FROM products WHERE PRODUCTID='".$id."'"); } //! A manipulator /** * Fetches a product as an associative array from the $dbobject * @return mixed */ function getProduct() { if ( $product=$this->dao->getRow() ) return $product; else return false; } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
有一点要注意的是,在模型和数据访问类之间,它们的交互从不会多于一行——没有多行被传送,那样会很快使程式慢下来。同样的程式对于使用模式的类,它只需要在内存中保留一行(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; } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
最后是控制器,我们将把视图实现为一个子类。
<?php /** * Controls the application */ class ProductController extends ProductView { //! A constructor. /** * Constucts a new ProductController object * @param $model an instance of the ProductModel class * @param $getvars the incoming HTTP GET method variables */ function ProductController (&$model,$getvars=null) { ProductView::ProductView($model); $this->header(); switch ( $getvars['view'] ) { case "product": $this->productItem($getvars['id']); break; default: if ( empty ($getvars['rownum']) ) { $this->productTable(); } else { $this->productTable($getvars['rownum']); } break; } $this->footer(); } } ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after login
注意这不是实现MVC的唯一方式——比如你可以用控制器实现模型同时整合视图。这只是演示模式的一种方法。
我们的index.php 文件看起来像这样:
MVC模式在网站架构中十分常见。它允许我们建立一个三层结构的应用程式,从代码中分离出有用的层,帮助设计师和开发者协同工作以及提高我们维护和扩展既有程式的能力。
视图(View)
“视图”主要指我们送到Web浏览器的最终结果——比如我们的脚本生成的HTML。当说到视图时,很多人想到的是模版,但是把模板方案叫做视图的正确性是值得怀疑的。
对视图来说,最重要的事情可能是它应该是“自我意识(self aware)”的,视图被渲染(render)时,视图的元素能意识到自己在更大框架中的角色。
以XML为例,可以说XML在被解析时,DOM API有着这样的认知——一个DOM树里的节点知道它在哪里和它包含了什么。 (当一个XML文档中的节点用SAX解析时只有当解析到该节点时它才有意义。)
绝大多数模板方案使用简单的过程语言和这样的模板标签:
<p>{some_text}</p> <p>{some_more_text}</p>Copy after login
它们在文档中没有意义,它们代表的意义只是PHP将用其他的东西来替换它。
如果你同意这种对视图的松散描述,你也就会同意绝大多数模板方案并没有有效的分离视图和模型。模板标签将被替换成什么存放在模型中。
在你实现视图时问自己几个问题:“全体视图的替换容易吗?”“实现一个新视图要多久?” “能很容易的替换视图的描述语言吗?(比如在同一个视图中用SOAP文档替换HTML文档)”
模型(Model)
模型代表了程序逻辑。(在企业级程序中经常称为业务层(business layer))
总的来说,模型的任务是把原有数据转换成包含某些意义的数据,这些数据将被视图所显示。通常,模型将封装数据查询,可能通过一些抽象数据类(数据访问层)来实现查询。举例说,你希望计算英国年度降雨量(只是为了给你自己找个好点的度假地),模型将接收十年中每天的降雨量,计算出平均值,再传递给视图。
控制器(controller)
简单的说控制器是Web应用中进入的HTTP请求最先调用的一部分。它检查收到的请求,比如一些GET变量,做出合适的反馈。在写出你的第一个控制器之前,你很难开始编写其他的PHP代码。最常见的用法是index.php中像switch语句的结构:
<?php switch (<blockquote>MVC模式在网站架构中十分常见。它Copy after login

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

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



Imagine an artificial intelligence model that not only has the ability to surpass traditional computing, but also achieves more efficient performance at a lower cost. This is not science fiction, DeepSeek-V2[1], the world’s most powerful open source MoE model is here. DeepSeek-V2 is a powerful mixture of experts (MoE) language model with the characteristics of economical training and efficient inference. It consists of 236B parameters, 21B of which are used to activate each marker. Compared with DeepSeek67B, DeepSeek-V2 has stronger performance, while saving 42.5% of training costs, reducing KV cache by 93.3%, and increasing the maximum generation throughput to 5.76 times. DeepSeek is a company exploring general artificial intelligence

AI is indeed changing mathematics. Recently, Tao Zhexuan, who has been paying close attention to this issue, forwarded the latest issue of "Bulletin of the American Mathematical Society" (Bulletin of the American Mathematical Society). Focusing on the topic "Will machines change mathematics?", many mathematicians expressed their opinions. The whole process was full of sparks, hardcore and exciting. The author has a strong lineup, including Fields Medal winner Akshay Venkatesh, Chinese mathematician Zheng Lejun, NYU computer scientist Ernest Davis and many other well-known scholars in the industry. The world of AI has changed dramatically. You know, many of these articles were submitted a year ago.

The performance of JAX, promoted by Google, has surpassed that of Pytorch and TensorFlow in recent benchmark tests, ranking first in 7 indicators. And the test was not done on the TPU with the best JAX performance. Although among developers, Pytorch is still more popular than Tensorflow. But in the future, perhaps more large models will be trained and run based on the JAX platform. Models Recently, the Keras team benchmarked three backends (TensorFlow, JAX, PyTorch) with the native PyTorch implementation and Keras2 with TensorFlow. First, they select a set of mainstream

Boston Dynamics Atlas officially enters the era of electric robots! Yesterday, the hydraulic Atlas just "tearfully" withdrew from the stage of history. Today, Boston Dynamics announced that the electric Atlas is on the job. It seems that in the field of commercial humanoid robots, Boston Dynamics is determined to compete with Tesla. After the new video was released, it had already been viewed by more than one million people in just ten hours. The old people leave and new roles appear. This is a historical necessity. There is no doubt that this year is the explosive year of humanoid robots. Netizens commented: The advancement of robots has made this year's opening ceremony look like a human, and the degree of freedom is far greater than that of humans. But is this really not a horror movie? At the beginning of the video, Atlas is lying calmly on the ground, seemingly on his back. What follows is jaw-dropping

Earlier this month, researchers from MIT and other institutions proposed a very promising alternative to MLP - KAN. KAN outperforms MLP in terms of accuracy and interpretability. And it can outperform MLP running with a larger number of parameters with a very small number of parameters. For example, the authors stated that they used KAN to reproduce DeepMind's results with a smaller network and a higher degree of automation. Specifically, DeepMind's MLP has about 300,000 parameters, while KAN only has about 200 parameters. KAN has a strong mathematical foundation like MLP. MLP is based on the universal approximation theorem, while KAN is based on the Kolmogorov-Arnold representation theorem. As shown in the figure below, KAN has

The latest video of Tesla's robot Optimus is released, and it can already work in the factory. At normal speed, it sorts batteries (Tesla's 4680 batteries) like this: The official also released what it looks like at 20x speed - on a small "workstation", picking and picking and picking: This time it is released One of the highlights of the video is that Optimus completes this work in the factory, completely autonomously, without human intervention throughout the process. And from the perspective of Optimus, it can also pick up and place the crooked battery, focusing on automatic error correction: Regarding Optimus's hand, NVIDIA scientist Jim Fan gave a high evaluation: Optimus's hand is the world's five-fingered robot. One of the most dexterous. Its hands are not only tactile

Target detection is a relatively mature problem in autonomous driving systems, among which pedestrian detection is one of the earliest algorithms to be deployed. Very comprehensive research has been carried out in most papers. However, distance perception using fisheye cameras for surround view is relatively less studied. Due to large radial distortion, standard bounding box representation is difficult to implement in fisheye cameras. To alleviate the above description, we explore extended bounding box, ellipse, and general polygon designs into polar/angular representations and define an instance segmentation mIOU metric to analyze these representations. The proposed model fisheyeDetNet with polygonal shape outperforms other models and simultaneously achieves 49.5% mAP on the Valeo fisheye camera dataset for autonomous driving

Even answering calls in Do Not Disturb mode can be a very annoying experience. As the name suggests, Do Not Disturb mode turns off all incoming call notifications and alerts from emails, messages, etc. You can follow these solution sets to fix it. Fix 1 – Enable Focus Mode Enable focus mode on your phone. Step 1 – Swipe down from the top to access Control Center. Step 2 – Next, enable “Focus Mode” on your phone. Focus Mode enables Do Not Disturb mode on your phone. It won't cause any incoming call alerts to appear on your phone. Fix 2 – Change Focus Mode Settings If there are some issues in the focus mode settings, you should fix them. Step 1 – Open your iPhone settings window. Step 2 – Next, turn on the Focus mode settings
