PHP object-oriented programming quick start

Source: Internet
Author: User
Tags php oop
PHP object-oriented programming quick start [abstract] object-oriented programming (OOP) is a basic skill in programming. PHP4 provides good support for OOP. How to use OOP to implement advanced PHP programming is of great significance for improving PHP programming capabilities and planning the Web development architecture.

Object-oriented programming (OOP) is a basic programming skill. PHP4 provides good support for OOP. How to use OOP to implement advanced PHP programming is of great significance for improving PHP programming capabilities and planning the Web development architecture. The following example illustrates the practical significance and application methods of php oop programming.

When we create a website with a database backend, we usually consider that the program must be applicable to different application environments. Different from other programming languages, in PHP, a data library is a specific function of a series of columns (if you do not use the ODBC interface ). Despite the high efficiency, the encapsulation is not enough. If there is a unified database interface, we can apply to a variety of databases without any modifications to the program, so that the program portability and cross-platform capabilities are greatly improved.

To complete OOP in PHP, object encapsulation is required, that is, writing classes. We can generate a new SQL class to implement simple and single database encapsulation. For example:
PHP:

The code is as follows:

Class SQL
{
Var $ Driver; // A subclass 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 ();
// Generate the corresponding database driver class 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 = $ passwd;
$ This-> Driver-> database = $ database;
$ 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 data database as an example. We write a database driver class MySQL. in this class, we further encapsulate all functions related to MySQL database operations. Put the file containing the class named MySQL. php in the include_path Directory of the PHP System, and you can use it normally. Note that when writing a database driver file, the file name should be consistent with the class name.
PHP:

The code is as follows:

Class MySQL
{
Var $ host;
Var $ user;
Var $ passwd;
Var $ database;
Function MySQL () // use constructors to initialize variables
{
$ Host = "";
$ User = "";
$ Passwd = "";
$ Database = "";
}

Function Connect ()
{
$ Conn = MySQL_connect ($ this-> host, $ this-> user, $ this-> passwd) or
Die ("cocould not con nect to $ this-> host ");
MySQL_select_db ($ this-> database, $ conn) or
Die ("cocould not swi tch to database $ this-> database ;");
Return $ conn;
}

Function Close ($ conn)
{
MySQL_close ($ conn );
}

Function Query ($ queryStr, $ conn)
{
$ Res = MySQL_query ($ queryStr, $ conn) or
Die ("cocould not que ry database ");
Return $ res;
}

Function getRows ($ res)
{
$ Rowno = 0;
$ Rowno = MySQL_num_rows ($ res );
If ($ rowno> 0)
{
For ($ row = 0; $ row <$ rowno; $ row ++)
{
$ Rows [$ row] = MySQL_fetch_row ($ res );
}
Return $ rows;
}
}
Function getRowsNum ($ res)
{
$ Rowno = 0;
$ Rowno = mysql_num_rows ($ res );
Return $ rowno;
}
}
?>



Similarly, to encapsulate other "database drivers" into our SQL classes, you only need to create the corresponding classes and name the driver file with the same name, and put it in the include directory of PHP.

After encapsulation, you can program the database in PHP according to the idea of OOP.

PHP:

The code is as follows:

Include ("SQL. php ");
$ SQL = new <font color = "# 0000bb"> SQL; // generate a new SQL object
If ($ SQL-> DriverRegister ("MySQL" & lt; font color = "#007700">) // register the database driver
{
$ SQL-> Connect ("localhost", "root" & l t; font color = "#007700">, "", "test" & l t; font color = "#007700"> );
$ Res = $ SQL-> query ("select & lt; font color =" #007700 "> * from test"); // return the query record set
$ Rowsnum = $ SQL-> getRowsNum ($ res );
If ($ rowsnum> 0)
{
$ Rows = $ SQL-> getRows ($ res );
Foreach ($ rows as $ row) // cyclically retrieve the record set content
{
Foreach ($ row as $ field ){
Print $ field ;}
}
}
$ SQL-> Close ();
}
?>



In practical applications, we can further expand various object classes based on actual needs. PHP also provides a series of complex OOP methods, such as inheritance, overload, reference, serialization, and so on. By fully mobilizing and using various methods, you can make your website more reasonable and structured, and make development and maintenance easier.

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.