In the PHP's forum, I always hear some people's OOP support to say three four four, saying that there is a defect, there is not enough, but I can't take the actual example. It turned out that I have to talk to you, but I have been very busy. Now I have a little more time, so I will take the frame of a project I have just done and explore. The 99% code of this project is written in an OOP method. It feels that PHP is very good for OOP, not generally good, is very good. Since the project itself is a commercial project, the source code is not good, but the basic framework can still be said, and the simplified example is more likely to understand some. If you don't know how to understand the OOP in PHP, let's use it, let's take a look at the manual, or the base reading is not too late. Anyway, this is a post without long legs and can't run. Long-term short talk, start now. I will use a simple example here, there is only one and a half functions. One is to send a "Hello, I can SAY OOP in PHP World!" To the browser, and half of the features is to make a query from the database and then output to the browser, saying that it is half function because it is just as an example. Tell the actual database operation. First introduce from my first file index.php. My index.php file is like this:
Code: Php incrude_once ('config.php'); include_once ('class.application.php'); $ app = & new application (); $ app-> run ();?>>
This is all, although there is only 4 lines, but if you write this way with OOP, it should be enough. A little experienced buddy will find that only an Application object is only used, so I really want to know what is the difference in this object? Let's continue to look at the inside of the document .Application.php. From the above code, we know that she should include at least two ways Application () and run (), so it should generally grow like this.
Code: Php class application {function application () {} function run () {}}?>
Even if you know what Application is, it seems that there is no way to complete our pre-set function? So I have to introduce how to run this program, all the pages in my structure are accessed through index.php and an Action parameter, for example, the first function should access index.php? Action = hellopage, and the second Features is accessible via index.php? Action = databasepage. Such a structure may be unfamiliar. So INDEX.PHP page should know what is passed by the Action parameters, that is, the Application object should know what this Action parameter is. So we need to add a method getAction () to Application to get the Action parameter. Since you know the action, you know what to do, then the method is Run () also knows how to go Run. At the same time, I can also treat (complete the function) every page as an object, so I should at least need two class class hellopage and class databasepage because the two objects are ultimately sending them to the browser. Some of them commented as their parent class Class Page below is three types of files. Class.Page.php
Code: PHP Class Page () {} Function Show () {// The method cannot be called directly to implement in the subclass. DIE ('You CAN NOT USE ");}}?> Where this Show method should be all of all page objects, just different in implementation. Class.hellopage.php
Code: Php require_once ("class.page.php"); class hellopage extends page {parent :: page ();} function show () {echo "Hello, i can say OOP in PHP WORLD ! ";"}}?>
Class.DatabasePage.php
Code: Php require_once ("class.page.php"); class databasepage extends Page {parent :: page ();} Function show () {// Do some database operations and then display the results . }}?>
At the same time, we also follow such a rule: the value of the action is consistent with the name of the called page class. For example, when an action = hellopage, the program needs to initialize a Hellopage object, which has such a rule and the above files. We can improve the Application class into this.
Code: PHP Class Application {Function Application () {} Function GetAction () {} Function Run () {$ PageClass = $ THIS-> GetAction (); include_once ("Class.". $ PageClass. "PHP" $ Page = & new $ PageClass (); $ Page-> show ();}}?>
Why is getAction () empty? Because it is too simple, you can easily write it out. See here, if you still don't quite understand, don't worry, you can stop again and again. If all understand, we will continue. We also have half a task that is not completed, so we need to improve our Application and page classes to complete the database operation function. You should first get a correct database connection before performing a database operation. If each page class that requires a database connection is really cost-time, it is not easy to maintain management and also destroy the original intention of OOP design. Page classes for performing database operations, such as DatabasePage, should only be done within it. Take a closer look, our design is not difficult to find the work of establishing a database connection to Application to do the most appropriate, so add a new member $ dB to Application and assign the established database connection to it when initialization.
Code: Php require_once ("class.database.php"); class application {var $ db; // database object function application () {$ this-> db = & new database (db_host, db_name, db_login, db_pass) // $ DB is now a database object} function getAction () {return $ _GET ['Action']; // Simple implementation GetAction;} Funciton & getDatabase () {RETURN $ THIS-> DB;} Function Run () {$ PageClass = $ THIS-> getAction (); include_once ("Class."); $ Page = & New $ PAGECLASS ($ this); // This is the only hand and foot Where this Application object is passed to the page object. $ page-> show ();}}?> You don't have to care about how to implement this Database object, know that it is an object containing a database connection, if you have used phplib, adoDB, or PEAR library It is easy to understand. This statement: $ this-> db = & new database (db_host, db_name, db_login, db_pass); it is to create a database connection. As for DB_HOST, DB_NAME, DB_LOGIN, DB_PASS these are constants that we have been set in advance in config.php. Since the database operation page DatabasePage needs a database connection, it also requires a variable $ dB to save the database object, so we need to improve the DatabasePage to this: Class.DatabasePage.php
Code: Php request_once ("class.page.php"); Class DatabasePage Extends Page {var $ db; function databasepage (& $ app) // Accept the Application object as a parameter. {Parent :: Page (); $ this-> db = $ app-> getDatabase (); // Get database objects in Application. } Function show () {$ sql = 'select * from sales_orders'; // Simple SQL example. $ Results = $ this-> db-> query ($ SQL); // Query is a public method of the Database object, submits SQL queries to the database. ...; // Do some operations to display the resulting results. }}?>