PHP object-oriented programming quick start

Source: Internet
Author: User
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 shows that PHP's O-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 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; // 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 );
}
}
? >

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.