【Abstract】Object-oriented programming (OOP) is a basic skill in our programming, and PHP4 provides good support for OOP. How to use OOP's idea to perform advanced programming in PHP is very meaningful for improving PHP programming capabilities and planning a good web development architecture.
Object-oriented programming (OOP) is a basic skill in our programming, and PHP4 provides good support for OOP. How to use OOP's idea to perform advanced programming in PHP is very meaningful for improving PHP programming capabilities and planning a good web development architecture. Below we will use examples to illustrate the practical significance and application methods of using PHP OOP for programming.
When we build a website with a database background, we will consider that the program needs to be suitable for different application environments. Different from other programming languages, in PHP, the operating database is a series of specific function functions (if you do not use the ODBC interface). Although this is very efficient, the packaging is not enough. If there is a unified database interface, then we can apply it to multiple databases without making any modifications to the program, which greatly improves the portability and cross-platform capabilities of the program.
To complete OOP in PHP, object encapsulation is required, that is, writing classes. We can implement a simple encapsulation of the database by generating a new SQL class. For example:
PHP:
Let’s take operating MySQL database as an example. We write a database driver class MySQL. In this class, we further encapsulate all functions related to MySQL database operations. Put the file containing the file name of this class under the include_path of the PHP system and it can be used normally. Note that when writing database driver files, the file name should be consistent with the class name.
PHP:
Similarly, we want to encapsulate other "database drivers" into our SQL class. We just need to create the corresponding class, name the driver file with the same name, and put it in the include directory of PHP.
After completing the encapsulation, you can implement the programming of the database in PHP according to the OOP's idea.
PHP:
In actual applications, we can further expand various object classes according to actual needs. In PHP, a series of complex OOP methods are also provided, such as inheritance, overloading, reference, serialization, etc. By fully mobilizing various methods and using them flexibly, your website can be more reasonable and structured, and it is easier to develop and maintain.
Object-oriented programming (OOP) is a basic skill in our programming, and PHP4 provides good support for OOP. How to use OOP's idea to perform advanced programming in PHP is very meaningful for improving PHP programming capabilities and planning a good web development architecture. Below we will use examples to illustrate the practical significance and application methods of using PHP OOP for programming.
When we build a website with a database background, we will consider that the program needs to be suitable for different application environments. Different from other programming languages, in PHP, the operating database is a series of specific function functions (if you do not use the ODBC interface). Although this is very efficient, the packaging is not enough. If there is a unified database interface, then we can apply it to multiple databases without making any modifications to the program, which greatly improves the portability and cross-platform capabilities of the program.
To complete OOP in PHP, object encapsulation is required, that is, writing classes. We can implement a simple encapsulation of the database by generating a new SQL class. For example:
PHP:
Copy the codeThe code is as follows:
<?
class SQL
{
var $Driver; //The actual database driver subclass
var $connection; //Shared database connection variable
function DriverRegister($d)
{
if($d!="")
{
$include_path = ini_get("include_path");
$DriverFile = $include_path."/".$d.".php";
//The storage path of the driver must be set in the INCLUDE_PATH in the file
if( file_exists( $DriverFile)) //Find out whether the driver exists
{
include($DriverFile);
$this->Driver = new $d();
// Generate corresponding database based on the driver name Driver class
return true;
}
}
return false; //Register driver failed
}
function Connect($host,$user,$passwd,$database)//Function to connect 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 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 row
{
return $this->Driver->getRows($res);
}
function getRowsNum($res)//Get the line number
{
return $this->Driver-> getRowsNum ($res);
}
}
?>
class SQL
{
var $Driver; //The actual database driver subclass
var $connection; //Shared database connection variable
function DriverRegister($d)
{
if($d!="")
{
$include_path = ini_get("include_path");
$DriverFile = $include_path."/".$d.".php";
//The storage path of the driver must be set in the INCLUDE_PATH in the file
if( file_exists( $DriverFile)) //Find out whether the driver exists
{
include($DriverFile);
$this->Driver = new $d();
// Generate corresponding database based on the driver name Driver class
return true;
}
}
return false; //Register driver failed
}
function Connect($host,$user,$passwd,$database)//Function to connect 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 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 row
{
return $this->Driver->getRows($res);
}
function getRowsNum($res)//Get the line number
{
return $this->Driver-> getRowsNum ($res);
}
}
?>
Let’s take operating MySQL database as an example. We write a database driver class MySQL. In this class, we further encapsulate all functions related to MySQL database operations. Put the file containing the file name of this class under the include_path of the PHP system and it can be used normally. Note that when writing database driver files, the file name should be consistent with the class name.
PHP:
Copy the codeThe code is as follows:
<?
Class MySQL
{
var $host;
var $user;
var $passwd;
var $database;
function MySQL() //Use constructor 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;
}
}
?>
Class MySQL
{
var $host;
var $user;
var $passwd;
var $database;
function MySQL() //Use constructor 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 other "database drivers" into our SQL class. We just need to create the corresponding class, name the driver file with the same name, and put it in the include directory of PHP.
After completing the encapsulation, you can implement the programming of the database in PHP according to the OOP's idea.
PHP:
Copy the codeThe code is as follows:
<?
Include(“”);
$sql = new < font color="#0000bb">SQL; //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"); //Return to the query record set
$rowsnum = $sql->getRowsNum($res);
if($rowsnum > 0)
{
$rows = $sql->getRows($res);
foreach($rows as $row) //Loop out the record set content
{
foreach($row as $field){
print $field;}
}
}
$sql->Close();
}
?>
Include(“”);
$sql = new < font color="#0000bb">SQL; //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"); //Return to the query record set
$rowsnum = $sql->getRowsNum($res);
if($rowsnum > 0)
{
$rows = $sql->getRows($res);
foreach($rows as $row) //Loop out the record set content
{
foreach($row as $field){
print $field;}
}
}
$sql->Close();
}
?>
In actual applications, we can further expand various object classes according to actual needs. In PHP, a series of complex OOP methods are also provided, such as inheritance, overloading, reference, serialization, etc. By fully mobilizing various methods and using them flexibly, your website can be more reasonable and structured, and it is easier to develop and maintain.