2009-10-06 115 views
71

我總是看到PHP中的變量$this,我不知道它用於什麼。我從來沒有親自使用它,搜索引擎忽略$,我最終搜索單詞「this」。

有人能告訴我變量$這是如何在PHP中工作?

回答

89

它是對當前對象的引用,它在面向對象代碼中最常用。

實施例:

<?php 
class Person { 
    public $name; 

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

$jack = new Person('Jack'); 
echo $jack->name; 

此存儲「傑克字符串作爲創建的對象的屬性。

+11

+1的術語「當前對象」,因爲它是描述的'$ this'有時非直觀的行爲更準確的方法。 – 2009-10-06 14:50:24

+0

在KillerPHP OOP教程中看到類似的示例代碼:) http://www.killerphp.com/tutorials/php-objects-page-1/ – Edson 2017-01-07 08:59:06

8

這是從本身內引用類的實例的方式,與許多其他面向對象的語言一樣。

PHP docs

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

0

它指的是當前類的實例,因爲計算器表示。

查看PHP Docs。這是在第一個例子下解釋的。

2

當你創建一個你有(在許多情況下)實例變量和方法(又名。函數)的類。 $ this訪問這些實例變量,以便你的函數可以接受這些變量,並做他們需要的任何事情來做任何你想做的事情。

MEDER的例子的另一個版本:

class Person { 

    protected $name; //can't be accessed from outside the class 

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

    public function getName() { 
     return $this->name; 
    } 
} 
// this line creates an instance of the class Person setting "Jack" as $name. 
// __construct() gets executed when you declare it within the class. 
$jack = new Person("Jack"); 

echo $jack->getName(); 

Output: 

Jack 
25

瞭解在PHP中$this變量的最好方法是問問PHP是什麼。不要問我們,問的編譯器:

print gettype($this);   //object 
print get_object_vars($this); //Array 
print is_array($this);   //false 
print is_object($this);   //true 
print_r($this);     //dump of the objects inside it 
print count($this);    //true 
print get_class($this);   //YourProject\YourFile\YourClass 
print isset($this);    //true 
print get_parent_class($this); //YourBundle\YourStuff\YourParentClass 
print gettype($this->container); //object 
+3

您忘記了幾個分號:D – smileBeda 2015-05-21 23:25:02

9

我知道它的老問題,反正約$這個另一個確切的解釋。 $ this主要用於引用類的屬性。

實施例:

Class A 
{ 
    public $myname; //this is a member variable of this class 

function callme() { 
    $myname = 'function variable'; 
    $this->myname = 'Member variable'; 
    echo $myname;     //prints function variable 
    echo $this->myname;    //prints member variable 
    } 
} 

輸出:

function variable 

member variable 
2

$這是一個特殊的變量,它指的是同一個對象即。本身。

它實際上是指當前類

這裏的實例是將清除上面的語句

<?php 
class Books { 
    /* Member variables */ 
    var $price; 
    var $title; 

    /* Member functions */ 
    function setPrice($par){ 
    $this->price = $par; 
    } 

    function getPrice(){ 
    echo $this->price ."<br/>"; 
    } 

    function setTitle($par){ 
    $this->title = $par; 
    } 

    function getTitle(){ 
    echo $this->title ." <br/>"; 
    } 
} 
?> 
+0

請詳細說明一下 – 2016-04-12 06:47:12

4

讓我們看看,如果我們不使用$這並儘量有實例變量會發生什麼情況爲例和 構造函數參數具有相同的名稱與下面的代碼片段

<?php 

class Student { 
    public $name; 

    function __construct($name) { 
     $name = $name; 
    } 
}; 

$tom = new Student('Tom'); 
echo $tom->name; 

?> 

它回聲不過

<?php 

class Student { 
    public $name; 

    function __construct($name) { 
     $this->name = $name; // Using 'this' to access the student's name 
    } 
}; 

$tom = new Student('Tom'); 
echo $tom->name; 

?> 

此相呼應「湯姆」

+2

您的代碼片段是完全一樣的,還是我錯過了什麼? – Demento 2016-06-19 15:40:49

+0

@Demento:是的。我修復了它,在第二個構造函數中使用了'$ this'。 – 2016-06-19 17:01:55