2010-12-21 104 views
8

您何時會在PHP中使用$this關鍵字?據我所知,$this是指在不知道對象名稱的情況下創建的對象。

此外關鍵字$this只能在一個方法內使用?

舉例說明,當您可以使用$this時,將會很棒。

+10

http://php.net/OOP - >基本知識。 – 2010-12-21 16:36:41

+3

特別是,請注意Example#2在這裏:http://php.net/manual/en/language.oop5.basic.php – RabidFire 2010-12-21 16:38:59

+0

`$ this`不是PHP中的關鍵字(但是是一個*僞變量*),所以你的問題不能回答。 – hakre 2012-08-04 11:27:26

回答

6

一個類可能包含自己的常量,變量(稱爲「屬性」)和函數(稱爲「方法」)。

<?php 
class SimpleClass 
{ 
    // property declaration 
    public $var = 'a default value'; 

    // method declaration 
    public function displayVar() { 
     echo $this->var; 
    } 
} 
?> 

的$的一些實例這個僞變量:

<?php 
class A 
{ 
    function foo() 
    { 
     if (isset($this)) { 
      echo '$this is defined ('; 
      echo get_class($this); 
      echo ")\n"; 
     } else { 
      echo "\$this is not defined.\n"; 
     } 
    } 
} 

class B 
{ 
    function bar() 
    { 
     // Note: the next line will issue a warning if E_STRICT is enabled. 
     A::foo(); 
    } 
} 

$a = new A(); 
$a->foo(); 

// Note: the next line will issue a warning if E_STRICT is enabled. 
A::foo(); 
$b = new B(); 
$b->bar(); 

// Note: the next line will issue a warning if E_STRICT is enabled. 
B::bar(); 
?> 

上例將輸出:

  • $這被定義(A)
  • $這是不定義。
  • $ this is defined(B)
  • $ this is not defined。
1

它在面向對象的編程(OOP)中使用:

<?php 
class Example 
{ 
    public function foo() 
    { 
     //code 
    } 

    public function bar() 
    { 
     $this->foo(); 
    } 
} 

僞變量$這是可用的時 的方法是從 對象上下文中調用。 $這是調用對象(通常是該方法所屬的對象 , ,但如果從輔助對象的 上下文中靜態調用 方法,可能還有另一個對象)的引用 。

2

如果您在PHP中進行面向對象的編程,那麼您只能使用$ this。意思是如果你正在創建類。這裏有一個例子:

class Item { 
    protected $name, $price, $qty, $total; 

    public function __construct($iName, $iPrice, $iQty) { 
    $this->name = $iName; 
    $this->price = $iPrice; 
    $this->qty = $iQty; 
    $this->calculate(); 
    } 

} 
1

有一次,我知道我最終使用的this等值的其他語言是實現「良好」接口;否則返回void的每個類方法將返回this,這樣可以方便地將方法調用鏈接在一起。

public function DoThis(){ 
    //Do stuff here... 
    return $this; 
} 
public function DoThat(){ 
    //do other stuff here... 
    return $this; 
} 

以上的可稱爲像這樣:

myObject->DoThis()->DoThat(); 

這對於一些有用的東西。

0

不,我認爲你的想法是錯誤的。當使用$this時,指的是同一類的對象。這樣

認爲我們有一個變量的值是$ var和THAT該對象的實例應設定爲5

$這個 - > VAR = 5;

3

最常見的用例是在面向對象編程中,同時在類中定義或工作。例如:

class Horse { 
    var $running = false; 

    function run() { 
     $this->running = true; 
    } 
} 

正如你所看到的,run函數中,我們可以使用$this變量指馬類,我們是「在」的實例。所以還有一點需要注意的是,如果你創建2個Horse類,那麼每個類中的$this變量將引用該Horse類的特定實例,而不是兩者。

1

用於當您想使用局部變量時。

您還可以從here瞭解更多關於它的信息。

function bark() { 
    print "{$this->Name} says Woof!\n"; 
} 
0

使用$這是引用屬於當前對象

$這 - 方法或實例變量>名= $命名 或 $這 - > callSomeMethod()

那是否要使用當前對象的子類別中實現的變量或方法。

如果您想具體調用父類的實現,你會做這樣的事情

父:: callSomeMethod()

1

$,這是用來對當前實例的引用一個東西。 所以你可以做這樣的事情:

class MyClass { 
    private $name; 

    public function setName($name) { 
     $this->name = $name; 
    } 

    //vs 
    public function setName($pName) { 
     $name = $pName; 
    } 
} 

而且另一個很酷的用途是,你可以鏈方法:

class MyClass2 { 
    private $firstName; 
    private $lastName; 

    public function setFirstName($name) { 
     $this->firstName = $name; 
     return $this; 
    } 

    public function setLastName($name) { 
     $this->lastName = $name; 
     return $this; 
    } 

    public function sayHello() { 
     print "Hello {$this->firstName} {$this->lastName}"; 
    } 
} 

//And now you can do: 
$newInstance = new MyClass2; 
$newInstance->setFirstName("John")->setLastName("Doe")->sayHello(); 
1

當您創建的對象的新實例$此使用。

例如,想象一下:

class Test { 
    private $_hello = "hello"; 

    public function getHello() { 
     echo $this->_hello; // note that I removed the $ from _hello ! 
    } 

    public function setHello ($hello) { 
     $this->_hello = $hello; 
    } 
} 

爲了訪問的方法getHello,我必須創建類測試的新實例,就像這樣:

$obj = new Test(); 
// Then, I can access to the getHello method : 
echo $obj->getHello(); 
// will output "hello" 

$obj->setHello("lala"); 
echo $obj->getHello(); 
// will output "lala"  

事實上,$這是在類內部使用的,當instancied。它被認爲是範圍

類裏面你使用$此(訪問* $ _你好*爲例),但在類外,$此並不是指你的類中的元素(如* $ _你好*),它是$ obj確實。

現在,$ OBJ之間的主要區別$此是因爲$ OBJ訪問您的類從,一些限制情況:如果你定義的東西私人保護在你的課,就像我的變量* $ _ *您好,$ OBJ不能訪問它(它是私有的!),但$這個可以,becase的$此Le在班級裏面。

-1
<?php 
    class identity { 
    public $name; 
    public $age; 
    public function display() { 
     return $this->name . 'is'. $this->age . 'years old'; 
    } 
    } 
?> 
相關問題