Quick Start to PHP programming

Source: Internet
Author: User
Tags php oop

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:

 
 
  1. <? Class SQL {var $ Driver;
  2. // The actual database driver subclass
  3. Var $ connection;
  4. // Shared database connection Variables
  5. Function DriverRegister ($ d ){
  6. If ($ d! = "") {$ Include_path = ini_get ("include_path ");
  7. $ DriverFile = $ include_path. "/". $ d. ". php ";
  8. // The path where the driver is stored must be in the INCLUDE_PATH set in the PHP. ini file.
  9. If (file_exists ($ DriverFile) // you can check whether the driver exists.
  10. {
  11. Include ($ DriverFile); $ this-> Driver = new $ d ();
  12. // Generate the corresponding database Driver Class Based on the driver name
  13. Return true;
  14. }
  15. }
  16. Return false;
  17. // Driver registration failed
  18. }
  19. Function Connect ($ host, $ user, $ passwd, $ database)
  20. // Database connection Function
  21. {$ This-> Driver-> host = $ host;
  22. $ This-> Driver-> user = $ user;
  23. $ This-> Driver-> passwd = $ pas swd;
  24. $ This-> Driver-> database = $ d atabase;
  25. $ This-> connection = $ this-> Driver-> Connect ();
  26. }
  27. Function Close ()
  28. // Close the Database Function
  29. {
  30. $ This-> Driver-> close ($ this-> connection );
  31. }
  32. Function Query ($ queryStr)
  33. // Database string query function {
  34. Return $ this-> Driver-> query ($ queryStr, $ this-> connection );
  35. }
  36. Function getRows ($ res)
  37. // Search for rows
  38. {
  39. Return $ this-> Driver-> getRows ($ res );
  40. }
  41. Function getRowsNum ($ res)
  42. // Obtain the row number
  43. {
  44. Return $ this-> Driver-> getRowsNum ($ res );
  45. }
  46. }
  47. ? >

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.

 
 
  1. <? Class MySQL {var $ host; var $ user; var $ passwd; var $ database;
  2. Function MySQL ()
  3. // Use constructors to initialize Variables
  4. {$ Host = "";
  5. $ User = "";
  6. $ Passwd = "";
  7. $ Database = "";
  8. }
  9. Function Connect ()
  10. {$ Conn = MySQL_connect ($ this-> host, $ this-> user, $ this-> passwd)
  11. Or die ("cocould not connect to $ this-> host ");
  12. MySQL_select_db ($ this-> database, $ conn) or die ("cocould not switch to database $ this-> database ;");
  13. Return $ conn;
  14. }
  15. Function Close ($ conn ){
  16. MySQL_close ($ conn );
  17. }
  18. Function Query ($ queryStr, $ conn)
  19. {
  20. $ Res = MySQL_query ($ queryStr, $ conn) or die ("cocould not query database ");
  21. Return $ res;
  22. }
  23. Function getRows ($ res)
  24. {
  25. $ Rowno = 0;
  26. $ Rowno = MySQL_num_rows ($ res );
  27. If ($ rowno> 0)
  28. {
  29. For ($ row = 0; $ row <$ rowno; $ row ++)
  30. {
  31. $ Rows [$ row] = MySQL_fetch_row ($ res );
  32. }
  33. Return $ rows;
  34. }
  35. }
  36. Function getRowsNum ($ res)
  37. {
  38. $ Rowno = 0;
  39. $ Rowno = mysql_num_rows ($ res); return $ rowno;
  40. }}? >

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.

 
 
  1. <? Include ("SQL. php ");
  2. $ SQL = new SQL;
  3. // Generate a new SQL object
  4. If ($ SQL-> DriverRegister ("MySQL "))
  5. // Register the database driver
  6. {
  7. $ SQL-> Connect ("localhost", "root", "", "test ");
  8. $ Res = $ SQL-> query ("select * from test ");
  9. // Return the query record set
  10. $ Rowsnum = $ SQL-> getRowsNum ($ res );
  11. If ($ rowsnum> 0 ){
  12. $ Rows = $ SQL-> getRows ($ res );
  13. Foreach ($ rows as $ row)
  14. // Cyclically retrieve record set content {
  15. Foreach ($ row as $ field) {print $ field ;}}$ SQL-> Close ();
  16. }? >

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.


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.