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?

回答1:

It\'s a reference to the current object, it\'s most commonly used in object oriented code.

  • Reference: http://www.php.net/manual/en/language.oop5.basic.php
  • Primer: http://www.phpro.org/tutorials/Object-Oriented-Programming-with-PHP.html

Example:

<?php
class Person {
    public $name;

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

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

This stores the \'Jack\' string as a property of the object created.



回答2:

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


回答3:

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


回答4:

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).



回答5:

Lets see what happens if we won\'t use $this and try to have instance variables and constructor arguments with the same name with the following code snippet

<?php

class Student {
    public $name;

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

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

?>

It echos nothing but

<?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;

?>

this echoes \'Tom\'



回答6:

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


回答7:

$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/>\";
  }
}
?> 


回答8:

$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).



回答9:

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

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



标签: php oop this