Basic knowledge of PHP Object-Oriented Programming

Source: Internet
Author: User

What we will introduce to you today is aboutNext we will illustrate the practical significance and application methods of using PHP object-oriented programming through examples.

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.

The completion of PHP object-oriented programming requires object encapsulation, that is, writing classes. We can implement simple database Encapsulation by generating a new SQL class. For example:

 
 
  1. <?
  2. Class SQL
  3. {
  4. Var $ Driver; // a subclass of the actual database Driver
  5. Var $ connection; // shared database connection variable
  6. Function DriverRegister ($ d)
  7. {
  8. If ($ d! = "")
  9. {
  10. $Include_path=Ini_get("Include_path ");
  11. $DriverFile= $ Include_path. "/". $ d. ". php ";
  12. // The path where the driver is stored must be in the INCLUDE_PATH set in the PHP. ini file.
  13. If (file_exists ($ DriverFile) // you can check whether the driver exists.
  14. {
  15. Include ($ DriverFile );
  16. $ This->Driver=New$ D ();
  17. // Generate the corresponding database Driver Class Based on the driver name
  18. Return true;
  19. }
  20. }
  21. Return false; // driver registration failed
  22. }
  23. Function Connect ($ host, $ user, $ passwd, $ database) // function used to Connect to the database
  24. {
  25. $ This->Driver->Host= $ Host;
  26. $ This->Driver->User= $ User;
  27. $ This->Driver->Passwd= $ Pas
  28. Swd;
  29. $ This->Driver->Database= $ D
  30. Atabase;
  31. $ This->Connection= $ This->Driver->Connect ();
  32. }
  33. Function Close () // closes the Database function
  34. {
  35. $ This->Driver->Close ($ this->Connection );
  36. }
  37. Function Query ($ queryStr) // database string Query function
  38. {
  39. Return $ this->Driver->Query ($ queryStr, $ this->Connection );
  40. }
  41. Function getRows ($ res) // find a row
  42. {
  43. Return $ this->Driver->GetRows ($ res );
  44. }
  45. Function getRowsNum ($ res) // obtain the row number
  46. {
  47. Return $ this->Driver->GetRowsNum ($ res );
  48. }
  49. }
  50. ?>

I hope that the above PHP object-oriented programming knowledge 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.