What does the $ this variable mean in PHP?

I see the $this variable in PHP all the time, and I have no idea what it is used for. I never used it personally, and search engines ignored $ , and I eventually found the word "this".

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

+66
oop php this
06 Oct '09 at 3:45
source share
9 answers

This is a reference to the current object most commonly used in object oriented code.

Example:

 <?php class Person { public $name; function __construct( $name ) { $this->name = $name; } }; $jack = new Person('Jack'); echo $jack->name; 

Saves the string "Jack" as a property of the created object.

+87
06 Oct '09 at 3:46
source share

The best way to learn about the $this variable in php is to ask PHP what it is. Do not 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 
+24
Dec 11 '13 at 18:23
source share

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

Example:

 Class A { $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 } } 

exit:

 function variable member variable 
+9
Feb 24 '15 at 6:41
source share

This is a way to refer to an instance of a class from within itself, as well as to many other object-oriented languages.

From the PHP docs :

The pseudo-variable $ this is available when the method is called from within the object context. $ 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 the secondary object).

+8
Oct 6 '09 at 3:47
source share

Let's see what happens if we don'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; ?> 

He does not hear anything except

 <?php class Student { public $name; function __construct( $name ) { $this->name = $name; // Using 'this' to access the student name } }; $tom = new Student('Tom'); echo $tom->name; ?> 

it repeats "tom"

+4
Jun 19 '16 at 2:59
source share

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

another version of the mediter 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 
+2
06 Oct '09 at 14:47
source share

$ is a special variable, and it refers to the same object, i.e. by itself.

it really refers to an instance of the current class

here is an example that 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/>"; } } ?> 
+2
Mar 26 '16 at 7:20
source share

$this reference to the calling object (usually the object to which this method belongs, but possibly another object if the method is called statically from the context of the secondary object).

+1
Oct 29 '15 at 14:18
source share

It refers to an instance of the current class, as stated in the medic .

See PHP docs . This is explained in the first example.

0
Oct 06 '09 at 3:47
source share



All Articles