Abstract object-oriented programming (OOP) is a fundamental skill in our programming, and PHP4 provides good support for OOP. How to use the idea of OOP for advanced programming of PHP is very meaningful for improving PHP programming ability and planning a good web development architecture.
Object-oriented programming (OOP) is a fundamental skill of our programming, and PHP4 provides good support for OOP. How to use the idea of OOP for advanced programming of PHP is very meaningful for improving PHP programming ability and planning a good web development architecture. In this example, we illustrate the practical meaning and application of programming oop 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.
To complete OOP in PHP, object encapsulation is required, which is to write classes. We can implement a simple encapsulation of the database by generating a new SQL class. For example:
Php:
Copy the Code code as follows:
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 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=$passwd;
$this->driver->database=$database;
$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);
}
}
?>
Let's take the MySQL database operation as an example. We write a database-driven MySQL class in which we encapsulate the functions of MySQL database operations further. Put the file containing the class, file name mysql.php in the PHP system include_path, it can be used normally. Note When you write a database driver file, the file name should be consistent with the class name.
Php:
Copy the Code code as follows:
Class MySQL
{
var $host;
var $user;
var $passwd;
var $database;
function MySQL ()//using constructors to implement variable initialization
{
$host = "";
$user = "";
$passwd = "";
$database = "";
}
function Connect ()
{
$conn = mysql_connect ($this->host, $this->user,$this->passwd) or
Die ("Could not con nect to $ this->host");
mysql_select_db ($this->database,$conn) or
Die ("Could not SWI tch to database $ this->database;");
return $conn;
}
function Close ($conn)
{
Mysql_close ($conn);
}
function Query ($QUERYSTR, $conn)
{
$res =mysql_query ($QUERYSTR, $conn) or
Die ("Could not que ry database");
return $res;
}
function GetRows ($res)
{
$rowno = 0;
$rowno = mysql_num_rows ($res);
if ($rowno>0)
{
for ($row=0;$row<$rowno;$row++)
{
$rows[$row]=mysql_fetch_row ($res);
}
return $rows;
}
}
function Getrowsnum ($res)
{
$rowno = 0;
$rowno = mysql_num_rows ($res);
return $rowno;
}
}
?>
Similarly, we want to encapsulate the other "database-driven" into our SQL class, only need to establish the corresponding class, and name the driver with the same name, put in the PHP include directory is OK.
Once the encapsulation is complete, the database can be programmed in PHP in accordance with the idea of OOP.
Php:
Copy the Code code as follows:
Include ("sql.php");
$sql = new < font color= "#0000bb" >SQL; To generate a new SQL object
if ($sql-> driverregister ("MySQL" & Lt;font color= "#007700" >))//Register database driver
{
$sql->connect ("localhost", "root" &l t;font color= "#007700", "", "Test" &l t;font color= "#007700" >);
$res=$sql->query ("Select & Lt;font color=" #007700 ">* from Test"); Returning a query record set
$rowsnum = $sql->getrowsnum ($res);
if ($rowsnum > 0)
{
$rows = $sql->getrows ($res);
foreach ($rows as $row)//Loop out recordset contents
{
foreach ($row as $field) {
Print $field;}
}
}
$sql->close ();
}
?>
In practical applications, we can further extend the various object classes according to the actual requirements. In PHP, there are also a number of complex OOP methods, such as inheritance, overloading, referencing, serialization, and so on. Making your site more rational and structured, and easier to develop and maintain, can be made possible by fully mobilizing the various methods and using them flexibly.
The above describes the PHP object-oriented programming QuickStart, including aspects of the content, I hope the PHP tutorial interested in a friend helpful.