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

首頁 > 語言 > PHP > 正文

php中this,parent,self關(guān)鍵字用法

2024-09-04 11:43:42
字體:
供稿:網(wǎng)友

this,parent,self都是在php中的class類中了,下面我來分別介紹this,parent,self在類中用應用與區(qū)別。

一,this

1,要用this,你必有是一個對像的形勢,不然它會報錯的,Fatal error: Using $this when not in object context.

2,this可以調(diào)用本類中的方法和屬性,也可以調(diào)用父類中的可以調(diào)的方法和屬性,代碼如下:

  1. <?php     
  2. class UserName    
  3. {     
  4. //定義屬性     
  5. private $name;    
  6. //定義構(gòu)造函數(shù)    
  7. function __construct( $name )    
  8. {    
  9. $this->name = $name;  
  10. //這里已經(jīng)使用了this指針    
  11. }    
  12. //析構(gòu)函數(shù)    
  13. function __destruct(){}     
  14. //打印用戶名成員函數(shù)    
  15. function printName()    
  16. {    
  17. print( $this->name );  
  18. //又使用了PHP關(guān)鍵字this指針    
  19. }    
  20. }     
  21. //實例化對象    
  22. $nameObject = new UserName 
  23. "heiyeluren" );     
  24. //執(zhí)行打印    
  25. $nameObject->printName(); 
  26.  //輸出: heiyeluren    
  27. //第二次實例化對象    
  28. $nameObject2 = new UserName( "PHP5" );    
  29. //執(zhí)行打印    
  30. $nameObject2->printName(); //輸出:PHP5    
  31. ?> 

我們看,上面的類分別在11行和20行使用了this指針,那么當時this是指向誰呢?其實this是在實例化的時候來確定指向誰,比如第一次實例化對象 的時候(25行),那么當時this就是指向$nameObject對象,那么執(zhí)行18行的打印的時候就把print($this-><name )變成了print($nameObject->name),那么當然就輸出了"heiyeluren".

二,self

self是指向類本身,也就是PHP self關(guān)鍵字是不指向任何已經(jīng)實例化的對象,一般self使用來指向類中的靜態(tài)變量.

1,self可以訪問本類中的靜態(tài)屬性和靜態(tài)方法,可以訪問父類中的靜態(tài)屬性和靜態(tài)方法。

2,用self時,可以不用實例化的,代碼如下

  1. <?php    
  2. class Counter    
  3. {    
  4. //定義屬性,包括一個靜態(tài)變量    
  5. private static $firstCount = 0;    
  6. private $lastCount;    
  7. //構(gòu)造函數(shù)    
  8. function __construct()    
  9. {    
  10. $this->lastCount = ++selft 
  11. ::$firstCount
  12.  //使用PHP self關(guān)鍵字來調(diào)用靜態(tài)變量,使用self 
  13. 調(diào)用必須使用::(域運算符號)    
  14. }    
  15. //打印最次數(shù)值    
  16. function printLastCount()    
  17. {    
  18. print( $this->lastCount );    
  19. }     
  20. }    
  21. //實例化對象    
  22. $countObject = new Counter();    
  23. $countObject->printLastCount(); 
  24.  //輸出 1 
  25. ?>  

三,parent

parent:: 可用于調(diào)用父類中定義的成員方法。 

parent::的追溯不僅于直接父類。 

通過parent::調(diào)用父類方法,代碼如下:

  1. <!-- 聲明一個員工類,經(jīng)理類繼承自員工類 --> 
  2. <? 
  3. class employee{ 
  4.  protected  $sal=3000;   
  5.  public function getSal(){ 
  6.   $this->sal = $this->sal + 1200;   
  7.   return $this->sal ; 
  8.  }  
  9.  
  10. class Manager extends employee { 
  11.  //如果想讓經(jīng)理在員工工資的基礎(chǔ)上多發(fā)1500元. 
  12.  //必須先調(diào)用父類的getSal()方法. 
  13.  public function getSal(){   
  14.   parent::getSal();// 這里調(diào)用了父類的方法. 
  15.   $this->sal = $this->sal + 1500;   
  16.   return $this->sal ; 
  17.  }  
  18. $emp = new employee(); 
  19. echo "普通員工的工資是 " . $emp->getSal(); 
  20. echo "<br>"
  21.  
  22. $manager = new Manager(); 
  23. echo "經(jīng)理的工資是: " . $manager->getSal(); 
  24. ?> 

parent是指向父類的指針,一般我們使用parent來調(diào)用父類的構(gòu)造函數(shù).

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 教育| 丰镇市| 贵州省| 东平县| 宁都县| 汝阳县| 祁连县| 疏附县| 金湖县| 延庆县| 察哈| 寿阳县| 汾阳市| 罗甸县| 乾安县| 图们市| 泽普县| 佛山市| 高邮市| 沭阳县| 威海市| 太白县| 集安市| 惠州市| 乌鲁木齐县| 万载县| 宁津县| 营口市| 延吉市| 乌兰县| 威远县| 资阳市| 昭觉县| 得荣县| 乐平市| 金塔县| 临汾市| 交口县| 光泽县| 新田县| 伊川县|