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:
- < ?
- Class SQL
- {
- var $Driver; Actual operation of the database-driven subclass
- var $connection; Shared Database connection variables
- function Driverregister ($d)
- {
- if ($d! = "")
- {
- $ include_path = Ini_get ("Include_path");
- $ Driverfile = $include _path. " /". $d.". PHP ";
- The drive's storage path must be set in the php.ini file include_path
- if (file_exists ($DriverFile))//Find out if the driver is present
- {
- Include ($DriverFile);
- $this- > Driver = New $d ();
- Generate the corresponding database driver class based on the driver name
- return true;
- }
- }
- return false; Registration Driver failed
- }
- Function Connect ($host, $user, $passwd, $database)//functions connected 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 ()//Close database functions
- {
- $this- > driver- > Close ($this->connection);
- }
- function query ($QUERYSTR)//database string query functions
- {
- return $this- > driver- > query ($QUERYSTR, $this->connection);
- }
- function GetRows ($res)//Find row
- {
- return $this- > driver- > getRows ($res);
- }
- function Getrowsnum ($res)//Get line number
- {
- return $this- > driver- > Getrowsnum ($res);
- }
- }
- ? >
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 ...