php 中class,PHP 中的類(class)

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

classPage{

var$Title;

var$Keywords;

var$Content;

functionDisplay( ) {

echo"nn";$this->DisplayTitle( );$this->DisplayKeywords( );

echo"nnn";

echo$this->Content;

echo"nnn";

}

functionDisplayTitle( ) {

echo"".$this->Title."n";

}

functionDisplayKeywords( ) {

echo'.$this->Keywords.'">';

}

functionSetContent($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= newPage; $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對面向物件編程語言提供的支援不但有利於我們設計程式的結構,對於對程式的維護也能提供很大的幫助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值