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 background, we usually considerProgramIt must be applicable to different application environments. And othersProgramming LanguageThe difference is that in PHP, database operations are a series of specific functions (if you do not use the ODBC interface ). Although this method is highly efficient, it is not encapsulated. If there is a unified database interface, we can apply to multiple databases without making 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 implement simple database Encapsulation by generating a new SQL class. For example:
<?
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 = $ 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 operations. Put the file containing this class, named mysql. php, and put it under the demode_path of the PHP system. This can be used normally. Note that when writing a database driver file, the file name should be consistent with the class name.
class mysql
{< br> var $ host;
var $ user;
var $ passwd;
var $ database;
function MySQL () // Use constructors to initialize variables
{< br> $ host = "";
$ user = "";
$ passwd = "";
$ database = "";
}< br> function connect ()
{< br> $ conn = mysql_connect ($ this-> host, $ this-> User, $ this-> passwd) or
die ("cocould not connect to $ this-> host");
mysql_select_db ($ this-> database, $ conn) or
die ("cocould not switch to database $ this-> database;");
return $ conn;
}< br> function close ($ conn)
{< br> mysql_close ($ conn);
}
Function query ($ querystr, $ conn)
{
$ Res = mysql_query ($ querystr, $ conn) or
Die ("cocould not query 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.
<?
Include ("SQL. php ");
$ SQL = new SQL; // generate a new SQL object
If ($ SQL-> driverregister ("MySQL") // register the database driver
{
$ SQL-> connect ("localhost", "root", "", "test ");
$ Res = $ SQL-> query ("select * 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.