Object-oriented programming (OOP) is a basic technique of programming. PHP4 provides good support for OOP. How to apply the idea of OOP to advanced PHP programming is very meaningful for improving PHP programming skills and planning the Web development architecture. Below I am object-oriented programming (OOP) is a basic skill in our programming. PHP4 provides good support for OOP. How to apply the idea of OOP to advanced PHP programming is very meaningful for improving PHP programming skills and planning the Web development architecture. In the following example, we will illustrate the practical significance and use of php oop programming.
When we create a website with a database backend, we usually consider that the program needs to be used in different use environments. Different from other programming languages, in PHP, a series of specific function functions are used to control the database (if you do not apply the ODBC interface ). This process is highly efficient, but not encapsulation. If there is a same database interface, we can not make any modifications to the program and apply it to multiple databases, so that the program portability and cross-platform can be greatly improved.
To complete OOP in PHP, object encapsulation is required, that is, writing classes. We can use a new SQL class to implement simple database encapsulation. For example:
<?
Class SQL
{
Var $ Driver; // The sub-class of the actual database Driver
Var $ connection; // shared database connection variable
Function DriverRegister ($ d)
{
If ($ d! = '')
{
$ Include_path = ini_get ('include _ path ');
$ DriverFile = $ include_path. '/'. $ d. '. php ';
// The path where the driver is stored must be in the INCLUDE_PATH set in the PHP. ini file.
If (file_exists ($ DriverFile) // You can check whether the driver exists.
{
Include ($ DriverFile );
$ This-> Driver = new $ d ();
// A database driver class is created based on the driver name.
Return true;
}
}
Return false; // driver registration failed
}
Function Connect ($ host, $ user, $ passwd, $ database) // function used to Connect to the database
{
$ This-> Driver-> host = $ host;
$ This-> Driver-> user = $ user;
$ This-> Driver-> passwd = $ pas
Swd;
$ This-> Driver-> database = $ d
Atabase;
$ This-> connection = $ this-> Driver-> Connect ();
}
Function Close () // closes the database function
{
$ This-> Driver-> close ($ this-> connection );
}
Function Query ($ queryStr) // database string Query function
{
Return $ this-> Driver-> query ($ queryStr, $ this-> connection );
}
Function getRows ($ res) // find a row
{
Return $ this-> Driver-> getRows ($ res );
}
Function getRowsNum ($ res) // Obtain the row number
{
Return $ this-> Driver-> getRowsNum ($ res );
}
}
? >
Take the MySQL database as an example. We write a database driver class MySQL. in this class, we further encapsulate all functions related to MySQL database control. Put the file containing this class, named MySQL. php, under the appsde_path of the PHP System, and the application will be normal. Note that when writing a database driver file, the file name should be consistent with the class name.
<?
Class MySQL
{
Var $ host;
Var $ user;
Var $ passwd;
Var $ database;
Function MySQL () // use a structured function to initialize a variable
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