国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php 類的初始化

2024-09-04 11:43:43
字體:
來源:轉載
供稿:網友
  1. class child  
  2. {  
  3.     private $parent;  
  4.   
  5.     function __construct($parent)  
  6.     {  
  7.         $this->parent = $parent;  
  8.     }  
  9.   
  10.     function getnationality()  
  11.     {  
  12.         return $this->parent->nationality;  
  13.     }  
  14. $parent = new parent('hispanic');  
  15. $child = new child($parent); 

php中是通過類來完成信息封裝的,在php中定義類的語法是:

  1. <?php 
  2. class class_name // 在面向對象編程類中,習慣上類的第一個字符為大寫,并且必須符合變量的命名規則. 
  3. //函數與變量的集合 
  4. }  
  5. ?> 

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

數據成員在類中使用"var"聲明來定義,在給數據成員賦值之前,它們是沒有類型的,一個數據成員可以是一個整數,一個數組,一個相關數組(associative array)或者是一個對象.

下面是一個類定義的實際例子,代碼如下:

  1. class student  
  2. {  
  3. var $str_name//姓名  
  4. var $str_sex//性別  
  5. var $int_id//學號  
  6. var $int_english//英語成績  
  7. var $int_maths//數學成績  

這是一個很普通定義類的簡單例子,用于顯示學生的學習成績,類名為student,student類包涵了一個學生的基本屬性:姓名、性別、學號、英語成績和數學成績.

function我們稱之為在類中被定義的函數,在函數中訪問類成員變量時,你應該使用$this->var_name,其中var_name 指的是類中被聲明的變量,否則對一個函數來說,它只能是局部變量,我們先定義一個input()的函數,用來給實例中的對象賦以初值,代碼如下:

  1. function input ( $name$sex$id$englis$maths)  
  2. {  
  3. $this->str_name=$name;  
  4. $this->str_sex =$sex;  
  5. $this->int_id =$id;  
  6. $this->int_englis=$english;  
  7. $this->int_maths=$maths;  
  8. }//開源代碼Vevb.com 

現在我們再定義一個叫“showinfo()”的函數,用于打印學生的基本情況,代碼如下:

  1. function showinfo() //定義showinfo()函數  
  2. {  
  3. echo (“姓名:$this->str_name 
  4. ”);  
  5. echo (“性別:$this->str_sex 
  6. ”);  
  7. echo (“學號:$this->int_id 
  8. ”);  
  9. echo (“英語成績:$this->int_english 
  10. ”);  
  11. echo (“數學成績:$this->int_maths 
  12. ”);  

而定義好的類則必須使用new關鍵詞來生成對象:$a_student=new student; 

例如我們要為一個名為$wing的對象創建實例,并進行賦值,可以使用下面的代碼:

$wing =new student; //用new關鍵詞來生成對象 

$wing ->input (“wing”,”男”,33,95,87);

分別輸入wing的姓名、性別、學號、英語成績、數學成績,其中姓名和性別是字符型變量,所以需要用雙引號,其它為數值型變量則不需要.

通過下面這段完整的源代碼,我們就可以很清楚的看到類在php是怎么被運用的,代碼如下:

  1. class student  
  2. {  
  3. var $str_name;  
  4. var $str_sex;  
  5. var $int_id;  
  6. var $int_english;  
  7. var $int_maths
  8. function input ( $name$sex$id$english$maths)  
  9. {  
  10. $this->str_name=$name;  
  11. $this->str_sex =$sex;  
  12. $this->int_id =$id;  
  13. $this->int_english=$english;  
  14. $this->int_maths=$maths;  
  15. }  
  16. function showinfo()  
  17. {  
  18. echo (“姓名:$this->str_name 
  19. ”);  
  20. echo (“性別:$this->str_sex 
  21. ”);  
  22. echo (“學號:$this->int_id 
  23. ”);  
  24. echo (“英語成績:$this->int_english 
  25. ”);  
  26. echo (“數學成績:$this->int_maths 
  27. ”);  
  28. }  
  29.  
  30. $wing = new student;  
  31. $wing->input (“wing”,”男”,33,95,87);  
  32. $paladin = new student;  
  33. $paladin->input (“paladin”,”女”,38,58,59.5); 
  34. $wing->showinfo();  
  35. $paladin->showinfo(); 

執行結果應是這樣的:

姓名:wing 
性別:男 
學號:33 
英語成績:95 
數學成績:87 
姓名:paladin 
性別:女 
學號:38 
英語成績:58 
數學成績:59.5

上一篇:php 模版設計模

下一篇:php類的魔術方法

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嘉荫县| 大港区| 鲁山县| 唐河县| 镇巴县| 西安市| 习水县| 鄂伦春自治旗| 双柏县| 永吉县| 遂平县| 宽甸| 丹江口市| 芦山县| 石家庄市| 本溪| 揭阳市| 五大连池市| 凤城市| 双柏县| 舟山市| 咸宁市| 桐庐县| 宁德市| 淳化县| 泊头市| 蕉岭县| 宣武区| 丹东市| 车致| 徐汇区| 敦化市| 深圳市| 洞口县| 高阳县| 三亚市| 盈江县| 得荣县| 余庆县| 台南县| 郎溪县|