PHP 中的類(class)

yeahokay發表於2007-11-26

在PHP裡我們可以定義一個類,類(Class)就是指變數與一些使用這些變數的函式的集合。PHP是一種鬆散型別的語言,所以透過型別過載不起作用,透過引數的個數不同來過載也不起作用。有時在面向中過載建構函式非常好,這樣你可以透過不同的方法建立物件(傳遞不同數量的引數)。在PHP中就是透過類來實現的。

class Page {
   var $Title;
   var $Keywords;
   var $Content;

   function Display( ) {
     echo "nn";
     $this->DisplayTitle( );
     $this->DisplayKeywords( );
     echo "nnn";
     echo $this->Content;
     echo "nnn";
   }

   function DisplayTitle( ) {
     echo "" . $this->Title . "n";
   }

   function DisplayKeywords( ) {
     echo '. $this->Keywords . '">';
   }

   function SetContent( $Data ) {
     $this->Content = $Data;
   }
 }
?>

Now, at first glance this looks pretty simple. And you know what? It is. Its just basic PHP code wrapped into a class. Let me point a few things out before we go any farther though.

VAR -
All variables declared in the class must be placed at the top of the class structure, and preceeded with the VAR statement.
$THIS -
$this is a variable that incidates the current object. That way, the object knows how to find itself while running functions contained within it.
For instance, $this->Keywords gets the data from the $Keywords variable in the object. You'll also notice that when using variables contained
within a class, you can't use the $ to reference them - But you have to use it to reference the object itself.

Lets save this class file out before we continue. If your following along, save it out as page.class for now. You'll need it for the example coming up.

Now that we have a class created, lets try using it in a normal script.

 include "page.class";

 $Sample = new Page;

 $Content = "

This page was generated by the Page Class example.

"
; $
Sample->Title = "Using Classes in PHP"; $Sample->Keywords = "PHP, Classes"; $Sample->SetContent( $Content ); $Sample->Display( ); ?>
[@more@]

在定義類時你可以按自已的喜好的格式進行定義,但最好能保持一種標準,這樣開發起來會更有效些。

  資料成員在類中使用"var"宣告來定義,在給資料成員賦值之前,它們是沒有型別的。一個資料成員可以是一個整數,一個陣列,一個相關陣列(Associative Array)或者是一個物件。

下面是一個類定義的實際例子:


CODE:
class Student
{
var $str_Name; //姓名
var $str_Sex; //性別
var $int_Id; //學號
var $int_English; //英語成績
var $int_maths; //數學成績
}
?>

  這是一個很普通定義類的簡單例子,用於顯示學生的學習成績,類名為Student,Student類包涵了一個學生的基本屬性:姓名、性別、學號、英語成績和數學成績。
  
  function我們稱之為在類中被定義的函式,在函式中訪問類成員變數時,你應該使用$this->var_name,其中var_name指的是類中被宣告的變數,否則對一個函式來說,它只能是區域性變數。 我們先定義一個Input()的函式,用來給例項中的物件賦以初值:


CODE:
function Input ( $Name, $Sex, $Id, $Englis, $Maths)
{
$this->str_Name=$Name;
$this->str_Sex =$Sex;
$this->int_Id =$Id;
$this->int_Englis=$English;
$this->int_Maths=$Maths;
}

現在我們再定義一個叫"ShowInfo()"的函式,用於列印學生的基本情況:


CODE:
function ShowInfo() //定義ShowInfo()函式
{
echo ("姓名:$this->str_Name

");
echo ("性別:$this->str_Sex

");
echo ("學號:$this->int_Id

");
echo ("英語成績:$this->int_English

");
echo ("數學成績:$this->int_Maths

");
}

而定義好的類則必須使用new關鍵字來生成物件:


CODE:
$A_student=new Student;

例如我們要為一個名為$Wing的物件建立例項,並進行賦值,可以使用下面的程式碼:


CODE:
$Wing =new Student; //用new關鍵字來生成物件
$Wing ->Input ("Wing","男",33,95,87);
//分別輸入Wing的姓名、性別、學號、英語成績、數學成績,其中姓名和性別是字元型變數,所以需要用雙引號,其他為數值型變數則不需要。

透過下面這段完整的原始碼,我們就可以很清楚的看到類在PHP是怎麼被哂玫模?br />

CODE:
class Student
{
var $str_Name;
var $str_Sex;
var $int_Id;
var $int_English;
var $int_maths;

function Input ( $Name, $Sex, $Id, $English, $Maths)
{
$this->str_Name=$Name;
$this->str_Sex =$Sex;
$this->int_Id =$Id;
$this->int_English=$English;
$this->int_Maths=$Maths;
}
function ShowInfo()
{
echo ("姓名:$this->str_Name

");
echo ("性別:$this->str_Sex

");
echo ("學號:$this->int_Id

");
echo ("英語成績:$this->int_English

");
echo ("數學成績:$this->int_Maths

");
}
}


$Wing = new Student;
$Wing->Input ("Wing","男",33,95,87);
$Paladin = new Student;
$Paladin->Input ("paladin","女",38,58,59.5);

$Wing->ShowInfo();
$Paladin->ShowInfo();

?>

執行結果應是這樣的:
姓名:Wing
性別:男
學號:33
英語成績:95
數學成績:87
姓名:Paladin
性別:女
學號:38
英語成績:58
數學成績:59.5

  PHP現有的版本較以前的版本在對面向物件程式設計的支援方面有了很大的改善,但支援的還不是很完整,不過現階段PHP對面向物件程式語言提供的支援不但有利於我們設計程式的結構,對於對程式的維護也能提供很大的幫助。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/786540/viewspace-984965/,如需轉載,請註明出處,否則將追究法律責任。

相關文章