Use of MVC in PHP

Source: Internet
Author: User

First, let me give an example:
A simpleArticleDisplay System
In simple cases, we assume that the article system is read-only. That is to say, this example will not involve the publishing of the Article. Now it is started.
Since it only involves reading databases, I have defined two interfaces.

Interface dataoperation
{
Public Function select ($ info );
Public Function selectnum ($ info );
}
The preceding interface defines the interface for reading data. The select method returns the required articles. The selectnum method returns the total number of articles, which is used for pagination. $ Info is an array used to store query conditions.
Interface datasource
{
Public static function getinstance ();
}
Here we assume that we operate on the database, and datasource defines an interface. All instance classes that implement this interface will get a static object.
Interface controller
{
Public Function POP ();
Public Function push ();
Public Function execute ();
}
Interface View
{
Public function display ();
}
Okay, let's implement it.
The following defines a class to implement the datasource interface. This class uses the singleton mode.
Class databasesource implements datasource
{
Public static $ instance = NULL;
Public static function getinstance ()
{
If (SELF: $ instance = NULL)
{
SELF: $ instance = new PDO ("MYSQL: host = localhost; dbname = article", "root", "123456 ");
}
Return self: $ instance;
}
}
Define an abstract class to implement dataoperation. We need to share a database connection, so I initialize this database object in the abstract class so that all subclasses can share this object.
Abstract class databaseoperation implements dataoperation
{
Protected $ db = NULL;
Public Function _ construct ()
{
$ This-> DB = databasesource: getinstance ();
}
Public Function select ($ info );
Public Function select ($ info );
}
Below I will write a business subclass to implement the abstract class databaseoperation
Class tech extends databaseoperation
{
Public Function select ($ info)
{
// Implement your Code
}
Public Function selectnum ($ info)
{
// Implement your code here
}
}
The business logic layer is implemented. The following is the control layer.
Class viewcontroller implements Controller
{
Private $ mod = array ();
Public Function push ($ key, $ value );
{
// Implement your code and register the class into $ this-> MOD;
}
Public Function POP ($ key)
{
// Implement your code and set $ this-> mod [$ key] to NULL;
}
Public Function execute ($ key)
{
// Implement your code here to generate an instance. Use the new features of PhP5 to handle exceptions.
}
}
Okay. The following is the presentation layer. The interface view will be implemented here.
Abstract articleview implements View
{
Protected $ smarty = NULL;
Public Function _ construct ()
{
$ This-> smarty = new smarty ();
/// You can define some property values of smarty below
}
}
A specific page, such as a display page of a tech article
Class techarticleview extends articleview
{
Public function display ()
{
// Implement your code, call the tech class and more databaseoperation subclasses
}
}
Okay. The following is the index. php entry.
Try
{
$ Viewcontroller = new viewcontroller ();
$ Viewcontroller-> push ("tech", techarticleview );
// Continuous increase
$ Mod = $ _ Get ["Mod"]: $ _ Get ["Mod"]: $ _ post ["Mod"];
// Last
$ Viewcontroller-> execute ($ key );
}
Catch (exception $ E)
{
// How to handle exceptions is yours
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.