专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »PHP教程 » class类:PHP 中的類(class) »正文

class类:PHP 中的類(class)

来源: 发布时间:星期三, 2008年9月10日 浏览:290次 评论:0

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

<?php
class Page {
var $Title;
var $Keywords;
var $Content;

function Display( ) {
echo \"<HTML>n<HEAD>n\";
$this->DisplayTitle( );
$this->DisplayKeywords( );
echo \"n</HEAD>n<BODY>n\";
echo $this->Content;
echo \"n</BODY>n</HTML>n\";
}

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

function DisplayKeywords( ) {
echo ’<META NAME=\"keywords\" CONTENT=\"’ . $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.

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

<?php
include \"page.class\";

$Sample = new Page;

$Content = \"<P>This page was generated by the Page Class example.</P>\";

$Sample->Title = \"Using Classes in PHP\";
$Sample->Keywords = \"PHP, Classes\"; [Page]
$Sample->SetContent( $Content );

$Sample->Display( );

?>

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

  資料成員在類中使用\"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 />

相关文章

读者评论

  • 共0条 分0页

发表评论

  • 昵称:
  • 内容: