1. 程式人生 > >php 面向對象:this 關鍵字

php 面向對象:this 關鍵字

func set 實例化 參數 避免 傳遞 析構函數 color 其它

PHP5中為解決變量的命名沖突和不確定性問題,引入關鍵字“$this”代表其所在當前對象。

$this在構造函數中指該構造函數所創建的新對象。

在類中使用當前對象的屬性和方法,必須使用$this->取值。

方法內的局部變量,不屬於對象,不使用$this關鍵字取值。

局部變量和全局變量與 $this 關鍵字

使用當前對象的屬性必須使用$this關鍵字。

局部變量的只在當前對象的方法內有效,所以直接使用。

註意:局部變量和屬性可以同名,但用法不一樣。在使用中,要盡量避免這樣使用,以免混淆。

 1 <?php
 2 class A{
 3     private $a = 99;
 4
//這裏寫一個打印參數的方法. 5 public function printInt($a){ 6 echo "這裏的 \$a 是傳遞的參數 $a "; 7 echo "<br>"; 8 echo "這裏的 \$this->a 是屬性 $this->a"; 9 } 10 } 11 12 $a = new A(); // 這裏的$a 可不是類中的任何一個變量了. 13 $a->printInt(88);

打印結果是

這裏的 $a 是傳遞的參數 88
這裏的 $this->a 是屬性 99

用$this調用對象中的其它方法

 1 <?php
 2 //寫一個類,讓他自動完成最大值的換算
 3 class Math{
 4     //兩個數值比較大小.
 5     public function Max($a,$b){
 6         return $a>$b?$a:$b;     
 7     }
 8     //三個數值比較大小.
 9     public function Max3($a,$b,$c){
10         //調用類中的其它方法.
11         $a = $this->Max($a,$b);
12         return $this
->Max($a,$c); 13 } 14 } 15 $math = new Math(); 16 echo "最大值是 ".$math->Max3(99,100,88);

打印結果是:

最大值是 100

使用$this調用構造函數

調用構造函數和析構函數的方法一致。

 1 <?php
 2 class A{
 3     private $a = 0;
 4     public function __construct(){
 5         $this->a = $this->a + 1 ;
 6     }
 7      
 8     public function doSomeThing(){
 9         $this->__construct();
10         return $this->a;
11     }     
12 }
13 $a = new A(); // 這裏的$a 可不是類中的任何一個變量了.
14 echo "現在 \$a 的值是" . $a->doSomeThing();

打印結果是:

現在 $a 的值是2

$this 到底指的什麽?

$this 就是指當前對象,我們甚至可以返回這個對象使用 $this

 1 <?php
 2 class A{
 3     public function  getASelf(){
 4         return $this;
 5     }
 6     public function __toString(){
 7         return "這是類A的實例.";     
 8     }  
 9 }
10 $a = new A(); // 創建A的實例;
11 $b = $a->getASelf(); //調用方法返回當前實例.
12 echo $a; //打印對象會調用它的__toString方法.

打印結果是:

這是類A的實例.

通過 $this 傳遞對象

 1 <!-- 通過$this 傳遞對象 
 2 在這個例子中,我們寫一個根據不同的年齡發不同工資的類.
 3 我們設置處理年齡和工資的業務模型為一個獨立的類.
 4 -->
 5 <?php
 6 class User{
 7     private $age ; 
 8     private $sal ;
 9     private $payoff ; //聲明全局屬性.
10     
11     //構造函數,中創建Payoff的對象.
12     public function __construct(){
13         $this->payoff = new Payoff();
14     }    
15     public function getAge(){
16         return $this->age;
17     }
18     public function setAge($age){
19         $this->age = $age;
20     }    
21     // 獲得工資.
22     public     function getSal(){
23         $this->sal =  $this->payoff->figure($this);
24         return $this->sal;
25     }
26 }
27 //這是對應工資與年齡關系的類.
28 class Payoff{
29     public function figure($a){
30         $sal =0;
31         $age = $a->getAge();
32         if($age >80 || $age <16 ){
33             $sal = 0;
34         }elseif ($age > 50){
35             $sal = 1000;
36         }else{
37             $sal = 800;
38         }
39         return $sal;
40     }
41 }
42 //實例化User
43 $user = new User();
44 
45 $user->setAge(55);
46 echo $user->getAge()."age ,his sal is " . $user->getSal();
47 echo "<br>";
48 
49 $user->setAge(20);
50 echo $user->getAge()."age , his sal is " . $user->getSal();
51 echo "<br>";
52 
53 $user->setAge(-20);
54 echo $user->getAge()."age , his sal is " . $user->getSal();
55 echo "<br>";
56 
57 $user->setAge(150);
58 echo $user->getAge()."age , his sal is " . $user->getSal();

打印結果是:

55age ,his sal is 1000
20age , his sal is 800
-20age , his sal is 0
150age , his sal is 0

轉載:http://www.nowamagic.net/php/php_KeywordThis.php

php 面向對象:this 關鍵字