PHP物件導向編程快速入門_php基礎

來源:互聯網
上載者:User
   物件導向編程(OOP)是我們編程的一項基本技能,PHP4對OOP提供了良好的支援。如何使用OOP的思想來進行PHP的進階編程,對於提高PHP編程能力和規劃好Web開發構架都是非常有意義的。下面我們就通過執行個體來說明使用PHP的OOP進行編程的實際意義和應用方法。 

  我們通常在做一個有資料庫背景網站的時候,都會考慮到程式需要適用於不同的應用環境。和其他程式設計語言有所不同的是,在PHP中,操作資料庫的是一系列的具體功能函數(如果你不使用ODBC介面的話)。這樣做雖然效率很高,但是封裝卻不夠。如果有一個統一的資料庫介面,那麼我們就可以不對程式做任何修改而適用於多種資料庫,從而使程式的移植性和跨平台能力都大大提高。 

  在PHP中要完成OOP,需要進行對象封裝,也就是編寫類。我們可以通過產生一個新的SQL類實現對資料庫的簡單封裝。例如: 

< ? 
class SQL 

var $Driver; //實際操作的資料庫驅動子類 
var $connection; //共用的資料庫連接變數 
function DriverRegister($d) 

if($d!="") 

$include_path = ini_get("include_path"); 
$DriverFile = $include_path."/".$d.".php"; 
//驅動的存放路徑必須在PHP.ini檔案中設定的INCLUDE_PATH下 
if( file_exists( $DriverFile)) //尋找驅動是否存在 

include($DriverFile); 
$this->Driver = new $d(); 
// 根據驅動名稱產生相應的資料庫驅動類 
return true; 


return false; //註冊驅動失敗 

function Connect($host,$user,$passwd,$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()//關閉資料庫函數 

$this->Driver->close($this->connection); 

function Query($queryStr)//資料庫字串查詢函數 

return $this->Driver->query($queryStr,$this->connection); 

function getRows($res)//尋找行 

return $this->Driver->getRows($res); 

function getRowsNum($res)//取得行號 

return $this->Driver-> getRowsNum ($res); 


? > 

  我們以操作MySQL資料庫為例。我們寫一個資料庫驅動類MySQL,在該類中,我們把有關MySQL資料庫操作的函數都做進一步的封裝。把包含該類,檔案名稱為MySQL.php的檔案放在PHP的系統 include_path下,就可以正常地使用了。注意編寫資料庫驅動檔案時,檔案名稱應和類名保持一致。

< ? 
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 connect to $this->host"); 
MySQL_select_db($this->database,$conn) or 
die("Could not switch 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 query 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的思想來實現對資料庫的編程了。

< ? 
Include(“SQL.php”); 
$sql = new SQL; //產生新的Sql對象 
if($sql-> DriverRegister(“MySQL”)) //註冊資料庫驅動 

$sql->Connect(“localhost”,”root”,””,”test”); 
$res=$sql->query(“select * 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方法,例如繼承,重載,引用,序列化等等。充分調動各種方法並靈活運用,就能夠使你的網站更合理和結構化,開發和維護也更容易。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.