Home >Backend Development >PHP Tutorial >Quick Start with PHP Object-Oriented Programming

Quick Start with PHP Object-Oriented Programming

WBOY
WBOYOriginal
2016-07-29 08:35:40869browse

[Abstract] Object-oriented programming (OOP) is a basic skill for our programming, and PHP4 provides good support for OOP. How to use OOP ideas to perform advanced programming of PHP is very meaningful for improving PHP programming capabilities and planning a good Web development architecture.
Object-oriented programming (OOP) is a basic skill for our programming, and PHP4 provides good support for OOP. How to use OOP ideas to perform advanced programming of 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's OOP for programming.
When we usually build a website with a database backend, we will consider that the program needs to be suitable for different application environments. What is different from other programming languages ​​is that in PHP, a series of specific functions are used to operate the database (if you do not use the ODBC interface). Although this is very efficient, the encapsulation is not enough. If there is a unified database interface, then we can apply it to a variety of databases without making any modifications to the program, thereby greatly improving the portability and cross-platform capabilities of the program.
To complete OOP in PHP, you need to implement object encapsulation, that is, write a class. We can achieve a simple encapsulation of the database by generating a new SQL class. For example:
PHP:

Copy the code The code is as follows:

class SQL
{
var $Driver; //Actual database driver subclass
var $connection; //Shared database Connection variables
function DriverRegister($d)
{
if($d!="")
{
$include_path = ini_get("include_path");
$DriverFile = $include_path."/".$d.". php";
//The driver storage path must be under the INCLUDE_PATH set in the PHP.ini file
if(file_exists($DriverFile)) //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; //Failed to register the driver
}
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 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 rows
{
return $this->Driver->getRows( $res);
}
function getRowsNum($res)//Get the row number
{
return $this->Driver-> getRowsNum ($res);
}
}
?>


We operate by MySQL database is an example. We write a database driver class MySQL. In this class, we further encapsulate the functions related to MySQL database operations. Put the file containing this class and the file name is MySQL.php under the PHP system include_path, 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 code The code is as follows:

Class MySQL 

var $host; 
var $user; 
var $passwd; 
var $database; 
function MySQL() //利用构造函数实现变量初始化 

$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; 


?> 


同样我们要封装其他的“数据库驱动”到我们 的SQL类中,只需要建立相应的类,并以同名命名驱动文件,放到PHP的include 目录就可以了。
完成封装以后,就可以 在PHP中按照OOP的思想来实现对数据库的编程了。
PHP:  

复制代码 代码如下:

Include(“SQL.php”); 
$sql = new < font color="#0000bb">SQL; //生成新的Sql对象 
if($sql-> DriverRegister(“MySQL”& lt;font color="#007700">)) //注册数据库驱动 

$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”); //返回查询记录集 
$rowsnum = $sql->getRowsNum($res); 
if($rowsnum > 0) 

$rows = $sql->getRows($res); 
foreach($rows as $row) //循环取出记录集内容 

foreach($row as $field){ 
print $field;} 


$sql->Close(); 

?> 

 
在实际应用中,我们还可以根据实际需求对各种对象类做进一步扩 展。在PHP中,还提供了一系列复杂的OOP方法,例如继承,重载,引用,串行化 等等。充分调动各种方法并灵活运用,就能够使你的网站更合理和结构化,开发 和维护也更容易。

以上就介绍了 PHP面向对象编程快速入门,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn