Object-Oriented Programming(OOP) is a basic programming skill,PHP4. 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 consider that the program needs to be applicable to different application environments. Different from other programming languages, in PHP, database operations are a series of specific functions (if you do not use ODBC interfaces ). 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;
- // The actual database driver subclass
- Var $ connection;
- // Shared database connection Variables
- 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)
- // Database connection Function
- {$ 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 ()
- // Close 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)
- // Search for rows
- {
- 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 {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 connect to $ this-> host ");
- MySQL_select_db ($ this-> database, $ conn) or die ("cocould not switch 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 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 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.
We hope that the above content will help you.