PHP basic knowledge of object-oriented programming _php tutorial

Source: Internet
Author: User
What we're introducing to you today is aboutIn the following example, we illustrate the practical meaning and application method of object-oriented programming using PHP.

When we do a Web site with a database background, we will take into account that the program needs to be applied to different environments. Unlike other programming languages, in PHP, the operations database is a series of specific function functions (if you do not use the ODBC interface). This is highly efficient, but the package is not enough. If there is a unified database interface, then we can not make any changes to the program to apply to a variety of databases, so that the program's portability and cross-platform capabilities are greatly improved.

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

 
 
  1. < ?
  2. Class SQL
  3. {
  4. var $Driver; Actual operation of the database-driven subclass
  5. var $connection; Shared Database connection variables
  6. function Driverregister ($d)
  7. {
  8. if ($d! = "")
  9. {
  10. $ include_path = Ini_get ("Include_path");
  11. $ Driverfile = $include _path. " /". $d.". PHP ";
  12. The drive's storage path must be set in the php.ini file include_path
  13. if (file_exists ($DriverFile))//Find out if the driver is present
  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; Registration Driver failed
  22. }
  23. Function Connect ($host, $user, $passwd, $database)//functions connected 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 ()//Close database functions
  34. {
  35. $this- > driver- > Close ($this->connection);
  36. }
  37. function query ($QUERYSTR)//database string query functions
  38. {
  39. return $this- > driver- > query ($QUERYSTR, $this->connection);
  40. }
  41. function GetRows ($res)//Find row
  42. {
  43. return $this- > driver- > getRows ($res);
  44. }
  45. function Getrowsnum ($res)//Get line number
  46. {
  47. return $this- > driver- > Getrowsnum ($res);
  48. }
  49. }
  50. ? >

I hope that the above introduction of the PHP object-oriented programming knowledge of the people are helpful.


http://www.bkjia.com/PHPjc/446417.html www.bkjia.com true http://www.bkjia.com/PHPjc/446417.html techarticle today we are going to introduce you to the following we have an example of the use of PHP object-oriented programming of the actual meaning and application methods. We usually do a database backstage ...

  • 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.