What does the variable $this mean in PHP?

2018-12-31 11:45发布

I see the variable $this in PHP all the time and I have no idea what it's used for. I've never personally used it, and the search engines ignore the $ and I end up with a search for the word "this".

Can someone tell me how the variable $this works in PHP?

标签: php oop this
9条回答
骚的不知所云
2楼-- · 2018-12-31 12:26

It refers to the instance of the current class, as meder said.

See the PHP Docs. It's explained under the first example.

查看更多
萌妹纸的霸气范
3楼-- · 2018-12-31 12:34

The best way to learn about the $this variable in php is to ask PHP what it is. Don't ask us, ask the compiler:

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
查看更多
浮光初槿花落
4楼-- · 2018-12-31 12:36

when you create a class you have (in many cases) instance variables and methods (aka. functions). $this accesses those instance variables so that your functions can take those variables and do what they need to do whatever you want with them.

another version of meder's example:

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
查看更多
低头抚发
5楼-- · 2018-12-31 12:37

I know its old question, anyway another exact explanation about $this. $this is mainly used to refer properties of a class.

Example:

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
   }
}

output:

function variable

member variable
查看更多
十年一品温如言
6楼-- · 2018-12-31 12:41

It is the way to reference an instance of a class from within itself, the same as many other object oriented languages.

From the PHP docs:

The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).

查看更多
时光乱了年华
7楼-- · 2018-12-31 12:42

$this is a special variable and it refers to the same object ie. itself.

it actually refer instance of current class

here is an example which will clear the above statement

<?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/>";
  }
}
?> 
查看更多
登录 后发表回答