What does oop php look like?

Source: Internet
Author: User
// From: http://bbs.chinaunix.net/forum/viewtopic.php? T = 224412

In the PHP forum, I always heard people say three things about PhP's OOP support, saying that there are flaws and deficiencies, but there are no actual examples. I have said that I want to talk about it with you, but I have been very busy. Now it takes some time to talk about it. So I will take out the framework of a project I just created and discuss it with you. 99% of the code of this project is written in OOP mode. I feel that PHP has very good support for Oop, but it is not very good. Because the project itself is a commercial project, the source code is hard to publish, but the basic framework can be said, and simplified examples are easier to understand. If you do not know much about OOP in PHP, stop it. It is not too late to read the manual or Basic Books first. It cannot be done without long legs.

Let's make a short speech. Start now. I will use a simple example here, with only one and a half functions. One is to send a sentence "hello, I can say OOP in PHP world! ", The other half function is to make a query from the database and then output it to the browser, saying that it is a half function because, as an example, there is no actual database operation.

First, let's start with index. php In my first file. My index. php file is like this:

Code:
<? PHP
Include_once ('config. php ');
Include_once ('class. application. php ');
$ APP = & New Application ();
$ App-> Run ();
?>


This is all. Although there are only four rows, it should be enough to write this in OOP mode.
Some experienced guys will find that only one application object is used here, so I really want to know what the object looks like? Let's continue to look at the internal file class. application. php. From the above Code, we know that she should contain at least two methods.
Application ()
And
Run ()
So it should be like this in general

Code:
<? PHP

Class Application
{
Function Application ()
{

}

Function run ()
{

}
}

?>


Now, even if you know what the application is, it seems that there is no way to complete our preset functions? So I also want 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 like this? Action = hellopage, while the second function is through index. php? Action = databasepage. You may not be familiar with this structure. Therefore, the index. php page should know what the passed action parameter is, that is, the application object should know what this action parameter is. Therefore, we need to add a method getaction () to the application to obtain the action parameter. Now that you know the action and what to do, the run () method knows how to run it.

At the same time, I can also treat every page that completes the function as an object, so I should need at least two classes
Class hellopage and
Class databasepage
Since these two objects eventually send pages to the browser, they propose their common parts as their parent class.
Class page
The content of the three types of files is as follows:

Class. Page. php

Code:
<? PHP

Class page
{
Function page ()
{

}

Function show ()
{
// The method cannot be called directly. It must be implemented in the subclass.
Die ('you can not use this funciton directly from page class ');
}
}
?>


The show method should be available to all page objects, but it is different in implementation.

Class. hellopage. php

Code:
<? PHP
Require_once ("class. Page. php ");

Class hellopage extends page
{
Function hellopage ()
{
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
{
Function databasepage ()
{
Parent: Page ();
}

Function show ()
{
// Perform some database operations and display the results.
}
}
?>

At the same time, we also observe this rule: the value of action is consistent with the name of the called page class. For example, when action = hellopage, the program knows that a hellopage object needs to be initialized, with such a rule and the above files, we can improve the application class to 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.

If you still don't understand it, you can stop and try again.

If it is clear, we will proceed. We still have half of the tasks not completed, so we need to improve our application and page class so that it can complete the database operation function.
Before performing database operations, you should first get a correct database connection. It is very time-consuming and labor-consuming to let every page class that requires database connection do this, it is not easy to maintain and manage, but it also damages the original design intention of OOP. The page class for database operations, such as databasepage, should only complete its internal work to obtain data. Taking a closer look at our design, it is not difficult to find that the work of establishing a database connection is best handled by the application, therefore, add a new member $ dB to the application and assign the database connection to it during 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 a database object now
}

Function getaction ()
{
Return $ _ Get ['action']; // simple implementation of getaction;
}

Funciton & getdatabase ()
{
Return $ this-> dB;
}

Function run ()
{
$ Pageclass = $ this-> getaction ();
Include_once ("class.". $ pageclass. ". php ");
$ Page = & new $ pageclass ($ this); // This is the only place with hands and feet. This application object is passed to the page object.
$ Page-> show ();
}
}

?>


You don't need to worry too much about how the database object comes from. You can know that it is an object containing database connections. If you have used phplib, ADODB, or the pear library is easy to understand.
This statement:
$ This-> DB = & new database (db_host, db_name, db_login, db_pass );
Is to establish a database connection.

Db_host, db_name, db_login, and db_pass are constants which have been pre-set in config. php.

Because database operation page databasepage requires database connection, it also requires a variable $ dB to save database objects, so we need to improve databasepage to this:

Class. databasepage. php

Code:
<? PHP
Require_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 (); // obtain the database object in the application.
}

Function show ()
{
$ SQL = 'select * From sale_orders '; // a simple SQL example.
$ Results = $ this-> DB-> query ($ SQL); // query is a common method of the database object. It submits SQL queries to the database.
...; // Do some operations to display the result.
}
}
?>


Well, half of the functions are complete. PHP supports OOP very well, with clear structure and convenient maintenance. As for efficiency, I don't see any loss, if you are interested, test it by yourself. With such a framework, you can easily cope with changes in various needs: adding various permission controls, separating the database layer, business logic, and representation layer, and increasing the number of remote call interfaces is not a problem, I just can't write so many things here. I really don't know who else has a reason to say that OOP is bad in PHP?

In addition, we need to remind you to use the & symbol when passing objects and assigning values to ensure that the same object is referenced each time.

// Original post discussion is very wonderful, it is recommended to refer to: http://bbs.chinaunix.net/forum/viewtopic.php? T = 224412

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.